Google Maps API V3:限制地图范围 [英] Google Maps API V3: limit map bounds

查看:102
本文介绍了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?

推荐答案

我有同样的问题,但应该将其整理出来相同的功能,听到的事件从移动或拖动改变为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天全站免登陆