javascript - 谷歌地图 : How to prevent InfoWindow from shifting the map

标签 javascript google-maps google-maps-api-3

使用 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>

屏幕截图:

disableAutoPan

关于javascript - 谷歌地图 : How to prevent InfoWindow from shifting the map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2488999/

相关文章:

javascript - 在浏览器打印上居中 Google map (V3)

google-maps - 谷歌地图上的多个标记

google-maps-api-3 - 拖动标记时更改方向渲染的位置

php - 谷歌地图API和PHP集成问题

javascript - JS游戏(Duck Hunt)将.png文件添加到特定级别

javascript - php表单提交与JS按钮的问题

json - 使用谷歌地点 api 检索地点/国家/地区的兴趣点(景点/旅游景点)

javascript - React-redux,连接函数不使用新数据更新状态

javascript - 在回调函数内部添加参数到回调函数

iPhone:使用 Google 获取路线