在Google地图中设置城市周围的边界 [英] Setting Boundaries around city in google maps

查看:163
本文介绍了在Google地图中设置城市周围的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,在Google Maps API V3中使用JavaScript在特定的城市/城镇周围设置边界



这种类型的内容甚至在API?



基本上我不希望我的用户能够跨越地图进一步跨越城市,包括国家地区不仅限于城市范围。

解决方案

您想提供给城市/城镇的边界可以完成。你只需要绘制多边形或多边形来指定城市边界的坐标,请访问 Gmap V3中覆盖图的演示和文档



请尝试以下代码以限制用户在整个区域进行平移。

  //北美的边界
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(28.70 ,-127.50),
new google.maps.LatLng(48.85,-55.90)
);

//监听dragend事件
google.maps.event.addListener(map,'dragend',function(){
if(strictBounds.contains(map.getCenter ())return;

//我们超出范围 - 将地图移回界限内

var c = map.getCenter(),
x = c.lng(),
y = c.lat(),
maxX = strictBounds.getNorthEast()。lng(),
maxY = strictBounds.getNorthEast()。lat() ,
minX = strictBounds.getSouthWest()。lng(),
minY = strictBounds.getSouthWest()。lat();

if(x if(x> maxX)x = maxX;
if(y if(y> maxY)y = maxY;

map.setCenter(new google.maps.LatLng(y,x));
});

这将限制用户跨越北美地区

I'm looking for a way to set boundaries around a specific city/town within the Google maps API V3 using JavaScript

Is this type of thing even supported within the API?

Basically i don't want my users to be able to pan across the map further then the city, including country area's not just city limits.

解决方案

well the boundry that you want to provide to the city/ town can be done. you just need to draw the polylines or polygons specifying the co ordinates of the boundries of the city.
visit demos and documentation for overlays in Gmap V3

try the following code to restrict user to pan across the region.

 // Bounds for North America
   var strictBounds = new google.maps.LatLngBounds(
     new google.maps.LatLng(28.70, -127.50), 
     new google.maps.LatLng(48.85, -55.90)
   );

   // Listen for the dragend event
   google.maps.event.addListener(map, 'dragend', function() {
     if (strictBounds.contains(map.getCenter())) return;

     // We're out of bounds - Move the map back within the bounds

     var c = map.getCenter(),
         x = c.lng(),
         y = c.lat(),
         maxX = strictBounds.getNorthEast().lng(),
         maxY = strictBounds.getNorthEast().lat(),
         minX = strictBounds.getSouthWest().lng(),
         minY = strictBounds.getSouthWest().lat();

     if (x < minX) x = minX;
     if (x > maxX) x = maxX;
     if (y < minY) y = minY;
     if (y > maxY) y = maxY;

     map.setCenter(new google.maps.LatLng(y, x));
   });

this will restrict user to pan across region of north america

这篇关于在Google地图中设置城市周围的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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