Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps: How to prevent InfoWindow from shifting the map

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 display, my entire map shifts so that the markers InfoWindow is centered.

I don't want my map to shift.

Question: How do I prevent the map from shifting when InfoWindows are near the end of the map border (which causes the InfoWindow by default to center & shift the map)?

like image 638
TeddyN Avatar asked Mar 21 '10 22:03

TeddyN


People also ask

How do I get rid of InfoWindow in Google Maps?

After constructing an InfoWindow, you must call open to display it on the map. The user can click the close button on the InfoWindow to remove it from the map, or the developer can call close() for the same effect.

What is InfoWindow in Google map?

An InfoWindow displays content (usually text or images) in a popup window above the map, at a given location. The info window has a content area and a tapered stem. The tip of the stem is attached to a specified location on the map. Info windows appear as a Dialog to screen readers.


2 Answers

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

like image 198
Daniel Vassallo Avatar answered Oct 13 '22 21:10

Daniel Vassallo


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>
like image 1
Siddharth Agrawal Avatar answered Oct 13 '22 23:10

Siddharth Agrawal