如何阻止谷歌地图api v3在北极或南极灰色区域的平移? [英] How to block google maps api v3 panning in the gray zone over north pole or under south pole?
问题描述
使用谷歌地图v3 JavaScript API我找不到阻止地图在北极或南极下平移的方法。
作为此页面上嵌入地图的示例:
https:// developers.google.com/maps/documentation/embed/guide
具有相同的行为,缩放世界视图并向北移动,使视图处于完全灰色区域。
如何防止在官方网站 http://maps.google.it ?
感谢geocodezip评论,我修改了Mike Williams为我的案例提供的解决方案。
以下是小提琴示例
相关代码部分:
google.maps.event.addListener(map,'center_changed',function(){
checkBounds( map);
});
//如果地图位置超出范围,请将其移回范围
函数checkBounds(map){
var latNorth = map.getBounds()。getNorthEast()。lat ();
var latSouth = map.getBounds()。getSouthWest()。lat();
var newLat;
if(latNorth< 85& latSouth> -85)/ *在两侧 - >没关系* /
return;
else {
if(latNorth> 85&&latSouth< -85)/ * out both side - >没关系* /
return;
else {
if(latNorth> 85)
newLat = map.getCenter()。lat() - (latNorth-85); / *太北,居中* /
if(latSouth <-85)
newLat = map.getCenter()。lat() - (latSouth + 85); / *太南,居中* /
}
}
if(newLat){
var newCenter = new google.maps.LatLng(newLat,map.getCenter().ng ());
map.setCenter(newCenter);
}
}
Using google maps v3 javascript API i cannot find a way to block the panning of the map over the north pole or under the south pole. As an example embedded maps on this page: https://developers.google.com/maps/documentation/embed/guide has the same behaviour, zooming out with the world view and panning north bring the view in a complete gray area.
How to prevent like it's done on official site http://maps.google.it ?
Thanks to geocodezip comments i modified Mike Williams' solution for my case.
Here is the fiddle example
Relevant code part:
google.maps.event.addListener(map, 'center_changed', function() {
checkBounds(map);
});
// If the map position is out of range, move it back
function checkBounds(map) {
var latNorth = map.getBounds().getNorthEast().lat();
var latSouth = map.getBounds().getSouthWest().lat();
var newLat;
if(latNorth<85 && latSouth>-85) /* in both side -> it's ok */
return;
else {
if(latNorth>85 && latSouth<-85) /* out both side -> it's ok */
return;
else {
if(latNorth>85)
newLat = map.getCenter().lat() - (latNorth-85); /* too north, centering */
if(latSouth<-85)
newLat = map.getCenter().lat() - (latSouth+85); /* too south, centering */
}
}
if(newLat) {
var newCenter= new google.maps.LatLng( newLat ,map.getCenter().lng() );
map.setCenter(newCenter);
}
}
这篇关于如何阻止谷歌地图api v3在北极或南极灰色区域的平移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!