javascript - 自动缩放和自动居中以在窗口中显示所有标记

标签 javascript google-maps

我正在使用谷歌地图通过标记显示位置。我正在将一组位置传递给 initialize()。我想设置自动缩放和自动居中来映射,以便所有标记都可见。 map 应采用缩放值,以便可以立即看到所有标记。 .这是我的代码:

     function initialize(arr) { 
      var myOptions = {
        zoom:13,
        center: arr[0],
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
      route = new google.maps.Polyline({
            path: arr,
            map:map
          });

              i=0;
              interval = setInterval(function() { 

                if(i<=arr.length)
                    {

                     marker = new google.maps.Marker({
                        position: arr[i],
                        map: map 

                      });
                     i++;

                    }//if

                else if(i>arr.length){

              window.clearInterval(interval);   
                }//else if
              },1000);//interval

}  //initialize

我有下面的代码,但不知道如何以及在哪里使用它。

         var latlngbounds = new google.maps.LatLngBounds();
            for (var i = 0; i < latlng.length; i++) {
                latlngbounds.extend(latlng[i]);
            }
            map.setCenter(latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds));

最佳答案

从您的问题来看,您似乎使用了多个标记并且您想设置缩放级别。

如果是这样,我的建议是看一下 fitBounds() .

fitBounds() method adjusts the map's viewport in order to view the passed LatLngBounds in full at the centre of the map.

查看此示例 fiddle用于演示。

关于javascript - 自动缩放和自动居中以在窗口中显示所有标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17939580/

相关文章:

javascript - <ul> 中的响应式 Canvas ?

javascript - google.maps.places 有时未定义

java - onMarkerClick 使用 switch Case

Javascript - 多维数组排序 - 字符串和 Int - 按 Int 排序

javascript - django 删除时没有 access-control-allow-origin header

javascript - AngularJS ng-class 过滤器不起作用

javascript - 如何让 jsFiddle 在 Chrome 中工作?

iPhone - 从纬度和经度获取城市名称

javascript - 仅 HTML/JS 和谷歌地图上的实时 GPS 跟踪器可以在手机上运行?可能吗?

html - 谷歌地图信息窗口高度不起作用