Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置地图缩放以覆盖所有可见标记?_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 如何设置地图缩放以覆盖所有可见标记?

Javascript 如何设置地图缩放以覆盖所有可见标记?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我已经创建了一个带有标记的地图,这些标记正在从mongodb集合获取latlongs数据。我已经将中心设置为特定位置,并将缩放级别设置为5,但我希望地图应该根据标记的位置放大或缩小。这样我就不必在中心指定任何特定位置。 请帮忙 守则如下: <div class="page-content"> <div id="tab-general"> <div id="map" style="height: 500px; wi

我已经创建了一个带有标记的地图,这些标记正在从mongodb集合获取latlongs数据。我已经将中心设置为特定位置,并将缩放级别设置为5,但我希望地图应该根据标记的位置放大或缩小。这样我就不必在中心指定任何特定位置。 请帮忙

守则如下:

 <div class="page-content">
            <div id="tab-general">
              <div id="map" style="height: 500px; width: 100%;">

              </div> 
            </div>
 </div>

 <script type="text/javascript">
//LOAD DATA FROM MONGO
       data= <%-JSON.stringify(data)%>

 </script>
 <script type="text/javascript">

 var LatLngs = [];
  for (j=0;j<data.length;j++){

     LatLngs[j] = [data[j].latitude, data[j].longitude, data[j].time, data[j].name];

  }

var map = new google.maps.Map(document.getElementById('map'), {
  center: new google.maps.LatLng(23.2500, 77.4170),
  zoom: 5,
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i ,ext;


for (i = 0; i < LatLngs.length; i++) { 
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(LatLngs[i][0], LatLngs[i][1]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent("Time - "+LatLngs[i][2]+"<br>User Name -"+LatLngs[i][3]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}

//从MONGO加载数据
数据=
var LatLngs=[];

对于(j=0;j您要做的是创建一个LatLngBounds对象。添加每个标记时,您将扩展边界以包括该标记的位置。添加所有标记后,您将更新地图以适应这些边界,它将自动调整其缩放,以便所有标记都可见

var bounds = new google.maps.LatLngBounds();

for (i = 0; i < LatLngs.length; i++) {
    position = new google.maps.LatLng(LatLngs[i][0], LatLngs[i][1]);

    marker = new google.maps.Marker({
        position: position,
        map: map
    });

    bounds.extend(position)
}

map.fitBounds(bounds);
var-bounds=new google.maps.LatLngBounds();
对于(i=0;i
完美的解决方案。谢谢。