gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:02:32 25 4
gpt4 key购买 nike

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

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

我怀疑我的 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);

最佳答案

可以这么说,这不是答案,而是我在 thread 上发现的一个(有点笨拙的)解决方法。在 Google Maps Javascript API v3 组中:

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

关于google-maps - 在 Google Maps API v3.0 中多次调用 map.fitBounds(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3873195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com