在Google Maps API中限制平移到一个地球仪

时间:2015-06-04 16:49:23

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

我有使用Google Maps API创建的地图,我想将平移限制在一个地球上;默认情况下,您可以连续向东/向西平移,地图会无休止地重复。我尝试使用发布到henningj's

this question解决方案
var allowedBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(85, -180),
    new google.maps.LatLng(-85, 180)
);
lastValidCenter = map.getCenter();

google.maps.event.addListener(map, "center_changed", function() {
    if (allowedBounds.contains(map.getCenter())) {
        lastValidCenter = map.getCenter();
        return; 
    }

    map.panTo(lastValidCenter);
});

我目前所拥有的内容不允许任何平移,对“'”的调用包含'总是失败。如果我记录map.getCenter()它看起来都很合理,我想(?!):

Object { A: 54.683366, F: 25.31663500000002 }

任何人都可以看到我做错了吗?

2 个答案:

答案 0 :(得分:1)

您的var allowedBounds = new google.maps.LatLngBounds( new google.maps.LatLng(85, -180), new google.maps.LatLng(-85, 180) ); 定义错误。它应该是(来自here):

  

LatLngBounds(sw?:LatLng,ne?:LatLng)从其西南角和东北角的点构造一个矩形。

此:

var allowedBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-85, -180),  //sw
    new google.maps.LatLng(85, 180)     //ne
); 

应该是:

fsm1 = fysom({
'initial':'Idle',
'events':[
    {'name':'sc','src':'Idle','dst':'percent30'},
    {'name':'sc','src':'percent30','dst':'percent60'},
    {'name':'sc','src':'percent60','dst':'percent90'},
    {'name':'sc','src':'percent90','dst':'Idle'}
    ],
'callbacks':{
    'onIdle':onIdle,
    'onRead':onpercent30,
    'onTurnon':onpercent60,
    'onDisplay':onpercent90
    }
    })  

      fsm2 = fysom({
    'initial':'SR',
    'events':[
        {'name':'Right','src':'SR','dst':'SL'},
        {'name':'Left','src':'SL','dst':'SR'}
        ],
    'callbacks':{
        'onIdle':onSR,
        'onRead':onSL
        }
})  

(负经度在正面以西,负纬度在正面以南)

the documentation

答案 1 :(得分:0)

您可以设置正确的缩放级别并重新设定最小缩放级别

      var mapOptions = {
        center: new google.maps.LatLng(50.431487, 30.539285),
        zoom: 2,
        minZoom: 2,
        maxZomm:19,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }