地图中心永远不会在允许范围内

时间:2015-04-30 08:18:39

标签: javascript google-maps google-maps-api-3

我有以下问题。我在我的网站上实现了google maps api v3。问题是以下几点。用户可以根据需要滚动顶部,这意味着他已经离开了可见的地图区域。

示例:http://woist.es/

如果你喘气到远处的顶部,你可以远远地在格陵兰岛上空并且有一个灰色区域。谷歌地图本身不允许这样做。

我在这里看到了这篇文章How do I limit panning in Google maps API V3?

并希望根据我的要求进行调整。喜欢这个

// bounds of the desired area
var allowedBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(85, -180),           // top left corner of map
    new google.maps.LatLng(-85, 180)
);
var lastValidCenter = map.getCenter();
// Add event listener to remove highlighted POI images
google.maps.event.addListener(map, 'center_changed', function() {
    if (allowedBounds.contains(map.getCenter())) {
        console.log('valid');
        // still within valid bounds, so save the last valid position
        lastValidCenter = map.getCenter();
    } else {
        console.log(map.getCenter());
        console.log('not valid');
        // not valid anymore => return to last valid position
        map.panTo(lastValidCenter);
    }
});

然而,我总是遇到其他情况。这是console.log(map.getCenter());

的示例结果
{
    B: 5.00233685
    k: 23.3818240
}

如何正确限制?

1 个答案:

答案 0 :(得分:0)

好的,我现在修好了。好像我把数字搞混了。

而不是

new google.maps.LatLng(85, -180),          
new google.maps.LatLng(-85, 180)

应该是

new google.maps.LatLng(-85, -180),           
new google.maps.LatLng(85, 180)

但我现在遇到的问题是,我仍然可以看到很多灰色区域。所以我将值调整为

new google.maps.LatLng(-73, -180),
new google.maps.LatLng(73, 180)
相关问题