我正在寻找一种使用 JavaScript 在 Google 地图 API V3 中设置特定城市/城镇周围边界的方法
API 中是否支持这种类型的事情?
基本上,我不希望我的用户能够比城市更远地平移地图,包括乡村地区而不仅仅是城市范围。
那么您想要为城市/城镇提供的边界就可以完成。您只需要绘制指定城市边界坐标的折线或多边形。
visit Gmap V3 中叠加的演示和文档
尝试以下代码来限制用户跨区域平移。
// 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));
});
这将限制用户跨越北美地区
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)