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
Google maps 在Google Maps API v3.0中多次调用map.fitBounds()_Google Maps_Fitbounds - Fatal编程技术网

Google maps 在Google Maps API v3.0中多次调用map.fitBounds()

Google maps 在Google Maps API v3.0中多次调用map.fitBounds(),google-maps,fitbounds,Google Maps,Fitbounds,我刚刚开始使用谷歌地图API(v3.0),到目前为止已经取得了很大的成功。我正在从数据库加载一组具有纬度和经度值的对象,将它们传递到脚本中,并在脚本中循环,以便将它们添加到地图中 我正在使用“bounds.extend()/map.fitBounds()”方法设置地图的缩放和边界(请参见下面的代码),这在第一次使用时就如预期的那样起作用;但是,如果清除现有标记,获取另一组对象,并在同一地图实例上执行相同的操作,则会错误地设置边界,通常会导致最小缩放(宇航员视图) 我的怀疑是,我的map对象对我给

我刚刚开始使用谷歌地图API(v3.0),到目前为止已经取得了很大的成功。我正在从数据库加载一组具有纬度和经度值的对象,将它们传递到脚本中,并在脚本中循环,以便将它们添加到地图中

我正在使用“
bounds.extend()
/
map.fitBounds()
”方法设置地图的缩放和边界(请参见下面的代码),这在第一次使用时就如预期的那样起作用;但是,如果清除现有标记,获取另一组对象,并在同一地图实例上执行相同的操作,则会错误地设置边界,通常会导致最小缩放(宇航员视图)

我的怀疑是,我的map对象对我给它的前一组边界有一些记忆,我需要在分配新边界之前找到清除这些边界的方法,但我真的不能太确定

非常感谢您的帮助

var locationList = [];
for (var i = 0; i < mapPoints.length; i++) { // mapPoints is a collection of DTOs
    var mapPoint = mapPoints[i];
    var location = new google.maps.LatLng(mapPoint.Latitude, mapPoint.Longitude);
    locationList.push(location);

    var marker = new google.maps.Marker({
        map: map,
        icon: '/Content/images/map/' + mapPoint.Status.Icon,
        shadow:  '/Content/images/map/shadow.png',
        position: location
    });
    markers.push(marker); // markers is an Array that is managed outside this loop
}

var bounds = new google.maps.LatLngBounds();
for (var j = 0; j < locationList.length; j++) 
    bounds.extend(locationList[j]);
map.fitBounds(bounds);
var locationList=[];
对于(var i=0;i
可以这么说,这不是答案,而是我在Google Maps Javascript API v3组中的a上发现的一个(稍微有点黑)解决方案:

//map.fitBounds(bounds);
setTimeout( function() { map.fitBounds( bounds ); }, 1 ); 

如果上面的答案不适用于您(我不适用),问题可能在于引导(假设您正在使用它)。当我在引导模式中嵌入一个map对象时,引导模式会产生各种古怪的行为。。奇怪的是,如果/当我在那里发出“警报”时。。在任何情况下,我通过构建自己的模式(即,不使用引导模式)解决了所有问题。

加载一组新标记时是否清除
locationList
变量?
locationList
每次都会初始化(代码示例的第一行)在循环开始之前也是空的。setTimeout或angular的$timeout在执行之前等待当前摘要周期完成,虽然我不确定为什么会这样,但我猜与fitBounds冲突的其他进程尚未完成。一个更好的选择可能是监听这个过程的完成事件,如果你知道它是什么的话。对我来说也很有用。奇怪,谢谢!我不得不用100毫秒而不是1毫秒来解决这个问题。此外,这给了我一个有趣的动画作为一个加号!