【问题标题】:auto zoom and auto center to show all markers in window自动缩放和自动居中显示窗口中的所有标记
【发布时间】:2013-07-30 03:51:06
【问题描述】:

我正在使用谷歌地图使用标记来显示位置。我正在传递一个位置数组来初始化()。我想设置自动缩放和自动中心来映射,以便所有标记都可见。地图应采用缩放值,以便可以一次看到所有标记。 .这是我的代码:

     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));

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

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

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

    fitBounds() 方法调整地图的视口以便查看 在地图中心完全通过了 LatLngBounds。

    查看此示例fiddle 进行演示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-14
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多