问题
I have this code that loads XML graphic markers, and I also set it to zoom when new markers are loaded. What I don't like however, is that the zoom happens "instantly" and doesnt zoom to level slowly, and also, for some reason the map zooms out much further than it needs too. I would like the map to zoom out only as far as it needs to so that the markers are in view.
Any suggestions? Thanks!
clearOverlays();
downloadUrl("AllActivityxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("id");
var address = markers[i].getAttribute("id");
var type = markers[i].getAttribute("venue_type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
markerBounds.extend(point);
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
markersArray.push(marker);
bindInfoWindow(marker, map, infoWindow, html);
map.fitBounds(markerBounds);
}
});
回答1:
zoom happens "instantly" and doesnt zoom to level slowly -- that is how it actually happens when you do fitBounds(). Looks like you want something like the map.panTo() which makes the transition on map slowly unlike the map.setCenter(), but that is not the case in fitBounds().
map zooms out much further than it needs too -- move the map.fitBounds(markerBounds); outside the for loop
来源:https://stackoverflow.com/questions/13442158/google-maps-does-not-zoom-organically-when-new-markers-are-loaded