怎么限制google地图的显示区域?

2025-04-23 23:25:41
推荐回答(1个)
回答1:

//goole mapv3

var minZoomLevel = 4;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: minZoomLevel,
center: new google.maps.LatLng(35.570335826274345, 106.53511059863281),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

//自定义地图缩放范围
google.maps.event.addListener(map, 'zoom_changed',
function() {
if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
});

//自定义地图的显示范围:中国区域内部
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(14.48003790418668, 66.28120434863283),
new google.maps.LatLng(54.44617552862156, 143.71284497363283
));
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 < 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));
});