Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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 fitbounds将覆盖所有可用的标记google map api_Javascript_Google Maps - Fatal编程技术网

Javascript fitbounds将覆盖所有可用的标记google map api

Javascript fitbounds将覆盖所有可用的标记google map api,javascript,google-maps,Javascript,Google Maps,我使用map.fitbounds缩放并居中地图以显示可用标记。这是我的代码 marker = new google.maps.Marker( { map:map, draggable:true, animation: google.maps.Animation.DROP, icon: icon }); marker2 = new google.maps.Marker( { map:map, draggable:true, animation: google.ma

我使用
map.fitbounds
缩放并居中地图以显示可用标记。这是我的代码

 marker = new google.maps.Marker(
 {
  map:map,
  draggable:true,
  animation: google.maps.Animation.DROP,
  icon: icon
 });

 marker2 = new google.maps.Marker(
 {
 map:map,
 draggable:true,
 animation: google.maps.Animation.DROP,
 icon:icon
 });

 var bounds = new google.maps.LatLngBounds();
 var markers = [marker, marker2];
 for (i = 0; i < markers.length; i++) {
 position = new google.maps.LatLng(markers[i][0], markers[i][1]);

 bounds.extend(position)
 }
map.fitBounds(bounds);
marker=new google.maps.marker(
{
地图:地图,
真的,
动画:google.maps.animation.DROP,
图标:图标
});
marker2=新的google.maps.Marker(
{
地图:地图,
真的,
动画:google.maps.animation.DROP,
图标:图标
});
var bounds=new google.maps.LatLngBounds();
var markers=[marker,marker2];
对于(i=0;i

但是上面的代码不起作用。我该如何解决它呢?

试试这个。我想你在bounds.extend()中使用了错误的变量…这段代码适合我

var createMarker = function (info){
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(info.latitude, info.longitude),
            map: $scope.map,
            animation: google.maps.Animation.DROP,


        });
        loc = new google.maps.LatLng(info.latitude, info.longitude);
        bounds.extend(loc);

    }  
    for (i = 0; i < markers.length; i++){
        createMarker(markers[i]);
    }
    map.fitBounds(bounds);
var createMarker=函数(信息){
var marker=new google.maps.marker({
位置:新google.maps.LatLng(信息纬度,信息经度),
map:$scope.map,
动画:google.maps.animation.DROP,
});
loc=新的google.maps.LatLng(信息纬度,信息经度);
边界扩展(loc);
}  
对于(i=0;i
谢谢你的代码。这是我的更新代码,
var bounds=new google.maps.LatLngBounds();var markers=[marker,marker2];var createMarker=function(info){var marker=new google.maps.marker({位置:new google.maps.LatLng(info.latitude,info.longitude),map:map,animation:google.maps.animation.DROP,});loc=new google.maps.LatLng(info.latitude,info.longitude);bounds.extend(loc);}对于(i=0;i