Google Maps API V3:限制地图边界 [英] Google Maps API V3: limit map bounds

查看:21
本文介绍了Google Maps API V3:限制地图边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置可以使用 Google Maps API V3 拖动地图的边界这是 V2 的解决方案 http://econym.org.uk/gmap/example_range.htm 效果很好.

I'm trying to set bounds where you can drag the map using Google Maps API V3 Here is the solution for V2 http://econym.org.uk/gmap/example_range.htm that works pretty well.

然而,API V3 并不是那么好:当你使用相同的 checkbounds() 函数时,当你到达边界时地图会抽搐,而 map.setCenter() 改变地图的中心.

However with API V3 it isn't so good: when you use the same checkbounds() function, the map is twitching when you reach the bound while map.setCenter() changes the center of the map.

如何解决?API V3 的解决方案是什么?

How to fix it? What is the solution for API V3?

推荐答案

我也遇到了同样的问题,不过这次应该可以解决了(功能一样,监听的事件从 'move' 或 '拖动'到'center_changed',就像一个魅力!:

I've had the same problem, but this should sort it out (it's the same function, the event to listen to is changed from 'move' or 'drag' to 'center_changed', works like a charm!:

google.maps.event.addListener(map,'center_changed',function() { checkBounds(); });

function checkBounds() {    
    if(! allowedBounds.contains(map.getCenter())) {
      var C = map.getCenter();
      var X = C.lng();
      var Y = C.lat();

      var AmaxX = allowedBounds.getNorthEast().lng();
      var AmaxY = allowedBounds.getNorthEast().lat();
      var AminX = allowedBounds.getSouthWest().lng();
      var AminY = allowedBounds.getSouthWest().lat();

      if (X < AminX) {X = AminX;}
      if (X > AmaxX) {X = AmaxX;}
      if (Y < AminY) {Y = AminY;}
      if (Y > AmaxY) {Y = AmaxY;}

      map.setCenter(new google.maps.LatLng(Y,X));
    }
}

这篇关于Google Maps API V3:限制地图边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆