使用 Google Maps API v3。
我注意到,如果我的 map 边框边缘附近有一个 map 标记...如果我单击 标记图标,以便显示信息窗口,我的整个 map 会移动,以便 标记信息窗口居中。
我不想让我的 map 移动。
问题:当 InfoWindow 靠近 map 边框末端时(这会导致 InfoWindow 默认居中并移动 map ),如何防止 map 移动?
最佳答案
由于您使用的是 v3,因此您可以使用 disableAutoPan
选项简单地阻止 infoWindow 移动 map ,如下例所示 ( API Reference ):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps disableAutoPan Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 200px; height: 200px"></div>
<script type="text/javascript">
var myLatlng = new google.maps.LatLng(37.44, -122.14);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
var infowindow = new google.maps.InfoWindow({
content: 'Test',
disableAutoPan: true
});
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Test Marker'
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
</script>
</body>
</html>
屏幕截图:
关于javascript - 谷歌地图 : How to prevent InfoWindow from shifting the map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2488999/