Google maps open info window by default?

柔情痞子 提交于 2019-11-29 20:36:56

If you want to show the info window opened by default without click, just add a new code like bellow:

Your code:

google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
});

So we have a new code like this:

google.maps.event.addListener(marker, 'click', function() {
   infowindow.open(map,marker);
});

infowindow.open(map,marker);

As you can see, we just add a little line code with infowindow.open(map,marker);

You can run "open" directly on page load should open the window automatically, without a click.

infowindow.open(map, marker);

However, this might not autopan the map properly in some cases, leaving the top of the overlay off the edge of the map.

A way that worked for me is to wrap it in a listener to the "tilesloaded" event on the map

google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
  infowindow.open(map, marker);
});
stevepowell2000

The code above works perfectly. However, the my map didnt shift around so that the infowindow was being fully shown. Instead, the pushpin stayed in the middle.

So how do you make the map adjust itself so the info window is fully viewable? Read this: Google Maps : auto center map on marker click

If you want the pushpin to stay where it is and not shift the map, read this: Google Maps: How to prevent InfoWindow from shifting the map

Hope that helps people take the next step

Below Code Is working fine for me:

Change latitude, longitude, Content and mapid (with your custom div id):

<script type="text/javascript">

function initialize() {
    var myLatlng = new google.maps.LatLng(48.206615,13.487928);
    var mapOptions = {
        zoom: 16,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    var contentString = '<p><strong>Bahnhofstraße 32</strong></p><p>Bahnhofstraße 32, 4910 Ried im</p><p>Innkreis, Austria</p>';

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


    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'Bahnhofstraße 32'
    });

    google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });
  // show map, open infoBox 
        google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
          infowindow.open(map, marker);
        });

}

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

    </script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!