2014-02-05 83 views
-1

我有一个自定义谷歌地图here,它有几个固定的位置。自定义谷歌地图不缩小

出于某种原因改变zoom值时,地图上没有缩小:

var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 8, 
     center: new google.maps.LatLng(59.265881,20.515594), 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     mapTypeControl: false, 
     streetViewControl: false, 
     panControl: false, 
     zoomControlOptions: { 
     position: google.maps.ControlPosition.LEFT_BOTTOM 
     } 
    }); 

任何想法可能会造成的呢?

休息的脚本:

var locations = [ 
     ['html', coordinates], 
     ['html', coordinates], 
     ['html', coordinates], 
     ['html', coordinates], 
     ['html', coordinates], 
    ]; 

    // Setup the different icons and shadows 
    var iconURLPrefix = 'http://www.bridgingthebaltic.org/wp-content/themes/dante-child/images/'; 

    var icons = [ 
     iconURLPrefix + 'map_icon.png', 
     iconURLPrefix + 'map_icon.png', 
     iconURLPrefix + 'map_icon.png', 
     iconURLPrefix + 'map_icon.png', 
     iconURLPrefix + 'map_icon.png', 
     iconURLPrefix + 'map_icon.png', 
    ] 
    var icons_length = icons.length; 


    var shadow = { 
     anchor: new google.maps.Point(15,33), 
     url: iconURLPrefix + 'map_icon.png' 
    }; 

    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 8, 
     center: new google.maps.LatLng(59.265881,20.515594), 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     mapTypeControl: false, 
     streetViewControl: false, 
     panControl: false, 
     zoomControlOptions: { 
     position: google.maps.ControlPosition.LEFT_BOTTOM 
     } 
    }); 

    var infowindow = new google.maps.InfoWindow({ 
     maxWidth: 450 
    }); 

    var marker; 
    var markers = new Array(); 

    var iconCounter = 0; 

    // Add the markers and infowindows to the map 
    for (var i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map, 
     icon : icons[iconCounter], 
     shadow: shadow 
     }); 

     markers.push(marker); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
     })(marker, i)); 

     iconCounter++; 
     // We only have a limited number of possible icon colors, so we may have to restart the counter 
     if(iconCounter >= icons_length){ 
     iconCounter = 0; 
     } 
    } 



    function AutoCenter() { 
     // Create a new viewpoint bound 
     var bounds = new google.maps.LatLngBounds(); 
     // Go through each... 
     $.each(markers, function (index, marker) { 
     bounds.extend(marker.position); 
     }); 
     // Fit these bounds to the map 
     map.fitBounds(bounds); 
    } 
    AutoCenter(); 

回答

1

出于某种原因,该地图不缩小时改变变焦值

原因是你的地图上的这个代码将导致其要始终缩放以适合计算的边界:

function AutoCenter() { 
     // Create a new viewpoint bound 
     var bounds = new google.maps.LatLngBounds(); 
     // Go through each... 
     $.each(markers, function (index, marker) { 
     bounds.extend(marker.position); 
     }); 
     // Fit these bounds to the map 
     map.fitBounds(bounds); 
    } 

如果您希望它使用zo在地图属性中设置,删除对该函数的调用。

+0

是的,你说得对。删除对AutoCenter功能的呼叫解决了我的问题。下一次,我会提醒自己在征求建议之前先阅读整个代码。 :) – Laniakea