我刚刚开始使用Google Maps 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);
有帮助吗?

解决方案

可以这么说,这不是答案,而是我在 线 在Google Maps JavaScript API V3组中:

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

其他提示

如果以上答案对您不起作用(对我不起作用),则问题可能在于引导程序(假设您正在使用它)。 Bootstrap模态在我嵌入地图对象时专门生成各种各样的奇怪行为。.奇怪的是,如果/当我在其中丢弃“警报”时,请纠正自己。.在任何情况下,我仅通过构建自己的模式来解决所有问题(即,不使用引导模式)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top