Javascript 地图中心从不在允许的范围内
我有以下问题。我已经在我的网站上实现了谷歌地图api v3。问题如下。用户可以按自己的意愿在顶部滚动,这意味着他将离开可视地图区域 例如: 如果你气喘吁吁地爬到远处的山顶,你可以在格陵兰岛上空盘旋很远,形成一个灰色区域。谷歌地图本身不允许这样做 我在这里看到了这个帖子 并想根据我的要求进行调整。像这样Javascript 地图中心从不在允许的范围内,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有以下问题。我已经在我的网站上实现了谷歌地图api v3。问题如下。用户可以按自己的意愿在顶部滚动,这意味着他将离开可视地图区域 例如: 如果你气喘吁吁地爬到远处的山顶,你可以在格陵兰岛上空盘旋很远,形成一个灰色区域。谷歌地图本身不允许这样做 我在这里看到了这个帖子 并想根据我的要求进行调整。像这样 // bounds of the desired area var allowedBounds = new google.maps.LatLngBounds( new google.map
// 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())的一个示例结果代码>
我怎样才能正确地限制它呢?好的,我现在就把它修好了。好像我把数字弄混了
而不是
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)
你设定的界限涵盖了整个世界是的,没错。用户应该能够看到整个世界,但不能再看得更远。如果你看到我发布的示例,你可以从地图中平移到灰色区域。我想禁止这是:LatLngBounds(sw?:LatLng,ne?:LatLng)
。西南角(第一个参数)应该是地图的左下角,东北角(第二个参数)应该是地图的右上角。你的评论不正确。Ye,评论只是复制粘贴的,没有编辑:p。我会把它删掉。感谢基本上所有这些基于中心的解决方案都是不够的,它们可能不会阻止贴图在指定边界之外平移。您必须检查当前边界的中心和东北+西南,只有当所有3个值都在指定边界内时,它才会始终按预期工作。
new google.maps.LatLng(-73, -180),
new google.maps.LatLng(73, 180)