1

I'm working with google maps api 3. It's a bit of annoyance, but how do I get infowindow.open after the marker and the map have loaded?

I've tried to add various listeners such as tilesloaded and idle and haven't had any joy.

In this working example you see the infowindow is loading before anything else: http://codepen.io/anon/pen/WvbexY

function initialize() {
    if (document.getElementById("maper")) {        
       var latlng = new google.maps.LatLng(52.370778, 4.899448);
       var mapOptions = {
         zoom: 11,
         center: latlng,
         scrollwheel: "",
         scaleControl: "",
         disableDefaultUI: "",
          mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var tinygmaps = new google.maps.Map(document.getElementById("maper"), mapOptions);
    var marker = new google.maps.Marker({
      map: tinygmaps,
      position: tinygmaps.getCenter()
    });

    var contentString = '<p>WHY ME FIRST?</p>';
    var infowindow = new google.maps.InfoWindow({
      content: contentString,
      position: latlng,
    });
    infowindow.open(tinygmaps, marker);
    //var openwindow = google.maps.event.addListener(tileListener, 'tilesloaded', open_infowindow); // Hummmm!
  }
}
google.maps.event.addDomListener(window, 'load', initialize);


function open_infowindow() {
  infowindow.open(tinygmaps, marker);
  google.maps.event.removeListener(tileListener);  
};
Community
  • 1
  • 1
orionrush
  • 566
  • 6
  • 30

1 Answers1

2

Edit: Changed the codepen to listen for tilesloaded before displaying the infowindow. The fork of your codepen with the tilesloaded listener is here: http://codepen.io/brenzy/pen/VLYwGN

Because SO needs some code:

google.maps.event.addListenerOnce(tinygmaps, 'tilesloaded', function() {
  // open the infowindow
});

On my machine, listening for both tilesloaded and idle appear to function the same. (Without either listener, the infowindow is displayed before the map.)

I am assuming that your version didn't work, because you missed the line

infowindow.open(tinygmaps, marker);

when you were refactoring, so the infowindow was being opened before the listener fired.

brenzy
  • 1,976
  • 11
  • 20
  • Ok I was just calling the listener wrong, but `idle` doesn't seem to make a difference. `tilesloaded` is working however (on chrome). Are you not finding the same? – orionrush Apr 25 '15 at 00:08
  • I am running on Chrome windows and the idle listener does make a difference. In the fiddle I forked, I the info window comes up after the map. However, they both come up very quickly, so it's subtle. I didn't try tilesloaded. – brenzy Apr 25 '15 at 00:32
  • My system isn't migtymouse, but on osx chrome, there is a significant difference (with your implementation) when using `idle` and `tilesloaded`. Maybe milage varies here, but `titlesloaded` seems better to me. If you're willing to describe why adding the listener as you did works where mine didn't (and drop in that working code from the fiddle) I'll mark your answer as correct. – orionrush Apr 25 '15 at 01:28