ego*_*rry 10 google-maps fitbounds
我刚刚开始使用谷歌地图API(v3.0),到目前为止取得了很大的成功.我正在从数据库加载一组具有纬度和经度值的对象,将它们传递到我的脚本中,并在脚本中循环它们以将它们添加到地图中.
我使用" bounds.extend()
/ map.fitBounds()
"方法设置地图的缩放和界限(见下面的代码),这是第一次按预期工作; 但是,如果我清除现有的标记,获取另一组对象,并在同一个地图实例上执行相同的操作,则会错误地设置边界,通常会导致最小缩放(宇航员的视图).
我的怀疑是我的地图对象有一些我已经给出的前一组边界的记忆,我需要找到一种方法来清除这些边界,然后再分配我的新边界,但我真的不能太确定.
任何帮助是极大的赞赏!
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);
Run Code Online (Sandbox Code Playgroud)
ego*_*rry 20
可以这么说,这不是答案,而是我在Google Maps Javascript API v3组中的一个主题上发现的(稍微有些hacky)解决方法:
//map.fitBounds(bounds);
setTimeout( function() { map.fitBounds( bounds ); }, 1 );
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6049 次 |
最近记录: |