【问题标题】:Google Maps: How to prevent InfoWindow from shifting the map谷歌地图:如何防止 InfoWindow 移动地图
【发布时间】:2011-01-30 03:31:43
【问题描述】:

使用 Google Maps API v3。

我注意到,如果我的地图边界边缘附近有一个地图标记...如果我单击 标记图标,以便显示信息窗口,我的整个地图移动,以便 标记信息窗口居中。

我不希望我的地图移动。

问题:当 InfoWindow 接近地图边界的末端时,如何防止地图移动(这会导致 InfoWindow 默认居中并移动地图)?

【问题讨论】:

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


    【解决方案1】:

    由于您使用的是 v3,您可以使用 disableAutoPan 选项简单地阻止 infoWindow 移动地图,如下例 (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>
    

    截图:

    【讨论】:

      【解决方案2】:

      agm-marker-info 有一个名为 [disableAutoPan] 的属性,将其设为 false 不会移动位于地图边界边缘附近的地​​图 InfoWindows。

      <agm-marker-info [disableAutoPan]="false">
          <b> Marker Info: "A" </b>
      </agm-marker-info>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-14
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-23
        相关资源
        最近更新 更多