Search code examples
google-mapsgoogle-maps-api-3google-maps-markers

How to change position of Google Maps infoWindow


enter image description here

As the title says, how can I change the position of an infoWindow like in the image above?

I've followed the instructions from Google. Now I want to change the infowindows position but can't figure out what to do.

function initialize() {
    var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
    var mapOptions = {
        zoom: 4,
        center: myLatlng
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var contentString = '<div id="content">' +
        '<div id="siteNotice">' +
        '</div>' +
        '<h1 id="firstHeading" class="firstHeading">Uluru</h1>' +
        '<div id="bodyContent">' +
        '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
        'sandstone rock formation in the southern part of the ' +
        'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) ' +
        'south west of the nearest large town, Alice Springs; 450&#160;km ' +
        '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major ' +
        'features of the Uluru - Kata Tjuta National Park. Uluru is ' +
        'sacred to the Pitjantjatjara and Yankunytjatjara, the ' +
        'Aboriginal people of the area. It has many springs, waterholes, ' +
        'rock caves and ancient paintings. Uluru is listed as a World ' +
        'Heritage Site.</p>' +
        '<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">' +
        'https://en.wikipedia.org/w/index.php?title=Uluru</a> ' +
        '(last visited June 22, 2009).</p>' +
        '</div>' +
        '</div>';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'Uluru (Ayers Rock)'
    });
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
    });
}

google.maps.event.addDomListener(window, 'load', initialize);

Solution

  • You can use the pixelOffset property.

    var infowindow = new google.maps.InfoWindow({
        pixelOffset: new google.maps.Size(200,0)
    });
    

    See https://developers.google.com/maps/documentation/javascript/reference#InfoWindowOptions

    Working code snippet:

    function initialize() {
    
      const mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(0, 0),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
    
      const map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    
      const marker = new google.maps.Marker({
        position: new google.maps.LatLng(0, 0),
        map: map,
      });
    
      const infowindow = new google.maps.InfoWindow({
        content: 'Hello world',
        pixelOffset: new google.maps.Size(200, 0)
      });
    
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
      });
    }
    #map-canvas {
      height: 180px;
    }
    <div id="map-canvas"></div>
    <script async src="//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize"></script>