Google Maps Does not Zoom “organically” when new markers are loaded

故事扮演 提交于 2020-01-07 09:34:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!