Google Maps: How to prevent InfoWindow from shifting the map

前端 未结 2 1231
我在风中等你
我在风中等你 2020-12-09 08:52

Using Google Maps API v3.

I noticed that if I have a map marker near the edge of my map border ... that if I click the marker icon so that the InfoWindow will displa

相关标签:
2条回答
  • 2020-12-09 09:08

    Since you are using v3, you can simply prevent the infoWindow from shifting the map with the disableAutoPan option, as in the following example (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>
    

    Screenshot:

    disableAutoPan

    0 讨论(0)
  • 2020-12-09 09:25

    There is a property called [disableAutoPan] of agm-marker-info, making it false will not shift your map InfoWindows which are located near the edge of map border.

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

    0 讨论(0)
提交回复
热议问题