我处于需要将夏威夷和阿拉斯加包括到googlemap中的情况,但是我添加了一个jquery,以防止将地图拖到美国Bounderies之外
可以将夏威夷和阿拉斯加移至北美总部附近吗?谢谢
这是防止地图拖到地图外部的一种方法
<script type="text/javascript">
var minZoomLevel = 5;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: minZoomLevel,
center: new google.maps.LatLng(38.50, -90.50),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// 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));
});
// Limit the zoom level
google.maps.event.addListener(map, 'zoom_changed', function () {
if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
});
</script>
最佳答案
您可以显示多个地图,该图显示美国的主要地图,以及两个带有阿拉斯加和夏威夷的小地图。
以下代码产生了它。
<script type="text/javascript">
function initialize()
{
var USA = new google.maps.LatLng(38.50, -90.50);
var ALASKA = new google.maps.LatLng(64.0000, -150.0000);
var HAWAII = new google.maps.LatLng(21.3114, -157.7964);
var myOptions =
{
zoom: 4,
center: USA,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var myOptions2 =
{
zoom: 3,
center: ALASKA,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var myOptions3 =
{
zoom: 6,
center: HAWAII ,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var map2 = new google.maps.Map(document.getElementById("map_canvas_2"), myOptions2);
var map3 = new google.maps.Map(document.getElementById("map_canvas_3"), myOptions3);
google.maps.event.addListener(map, "zoom_changed", function()
{
zoomLevel = map.getZoom();
map2.setZoom(zoomLevel);
map3.setZoom(zoomLevel);
})
google.maps.event.addListener(map, "drag", function()
{
centre = map.getCenter();
map2.setCenter(centre);
map3.setCenter(centre);
})
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="top: 5px; left: 30px; width:800px; height:600px; float: left"></div>
<div id="map_canvas_2" style="top: 5px; left: 75px; width:300px; height:300px"></div>
<div id="map_canvas_3" style="top: 15px; left: 75px; width:300px; height:300px"></div>
</body>