【发布时间】:2009-07-08 13:19:48
【问题描述】:
我怎样才能让你不能在谷歌地图中拖到英国以外的地方? 你能把它锁定在某个区域吗,因为我希望能够在英国周围拖动,但不能在外面拖动。
塔
克里斯
【问题讨论】:
标签: api google-maps locking geolocation
我怎样才能让你不能在谷歌地图中拖到英国以外的地方? 你能把它锁定在某个区域吗,因为我希望能够在英国周围拖动,但不能在外面拖动。
塔
克里斯
【问题讨论】:
标签: api google-maps locking geolocation
有关如何执行此操作的文章,请参阅 Google Maps API Range Limiting。
代码都在the example中。
【讨论】:
较早接受的答案在 v3 中已弃用,但在 2019 年 2 月,Google 提供了“限制地图边界”API。
https://developers.google.com/maps/documentation/javascript/examples/control-bounds-restriction
锁定波兰边界的示例代码是:
var centerPoland = { lat: 52.407578, lng: 19.393004 };
function initMap() {
var options = {
zoom: 7,
center: centerPoland,
restriction: {
latLngBounds: {
east: 24.111384999999927,
north: 54.83666,
south: 49.002914,
west: 14.149166000000037
},
strictBounds: true
},
scaleControl: true
};
// Create map
map = new google.maps.Map(document.getElementById("map"), options);
}
请注意,您无法缩小以查看整个国家(包括邻国)。那是因为strictBounds: true 设置为false 可以缩小,但平移限制仍然存在。
【讨论】: