Google maps dynamically zooming in different locat

2019-06-12 10:54发布

Hi I need to create a method where the camera zooms for 5 seconds in a location and then dynamically changes to a location that I set. I ve implementing the zooming function where it zooms dynamically every 5000 ml but anyone can help on how I can make it for instance change location pause for 5sec and then rezoom to that location? Many thanks. here is my code.

var map;

function initialize() {
   var latlng = new google.maps.LatLng(52.474, -1.868);

   var myOptions = {
      zoom: 1,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
   };

   var bikeLayer = new google.maps.BicyclingLayer();
   bikeLayer.setMap(map);

   var curZoom = 1;
   var zoomInterval;

   // create map with zoom level curZoom
   // ...

   zoomInterval = setInterval(function () {
      curZoom += 1;
      map.setZoom(curZoom);
      if (curZoom === 6) {
         clearInterval(zoomInterval);
      }
   }, 5000);
}

google.maps.event.addDomListener(window, 'load', initialize);

1条回答
forever°为你锁心
2楼-- · 2019-06-12 11:37

Use map.setCenter() in your setInterval() function. You probably have predefined locations, so you need to use them in setCenter() function.

Example usage with predefined locations:

var places = [ [52, -1], [52, -2], [53, -1], [53, -2], [54, -1], [54, -2] ];

zoomInterval = setInterval(function () {
   var lat = places[curZoom][0];
   var lng = places[curZoom][1];
   map.setCenter(new google.maps.LatLng(lat, lng));
   map.setZoom(curZoom); // or a static value like 5, 7 or something else
   if (curZoom === 6) {
      curZoom = 0;
   } else {
      curZoom++;
   }
}, 5000);
查看更多
登录 后发表回答