我使用這個例子:谷歌地圖嚴厲的限制,進行平
Google Maps v3 - limit viewable area and zoom level
問題是,我決定在我的自定義地圖的SW和NE分,但它似乎只禁止任何基於搖攝地圖的中心,這允許在嚴格界限之外進行平移。
這裏是我的代碼:
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(sw_lat, sw_lon),
new google.maps.LatLng(ne_lat, ne_lon)
);
google.maps.event.addListener(map, 'drag', 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));
});
你有這方面的一個例子嗎? –
查看我的更新回答 – duncan
這似乎並不奏效。光標被固定在地圖上,我無法停止平移。 –