简体   繁体   English

谷歌地图:地图和标记加载后打开信息窗口

[英]google maps: open infowindow after map & marker load

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? 我正在使用google maps api3。这有点烦人,但是在标记和地图加载如何获取infowindow.open

I've tried to add various listeners such as tilesloaded and idle and haven't had any joy. 我尝试添加各种侦听器,例如tilesloadedidle ,并且没有任何乐趣。

In this working example you see the infowindow is loading before anything else: http://codepen.io/anon/pen/WvbexY 在这个工作示例中,您会看到infowindow首先加载: 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);  
};

Edit: Changed the codepen to listen for tilesloaded before displaying the infowindow. 编辑:更改了代码笔以在显示信息窗口之前侦听加载的tile。 The fork of your codepen with the tilesloaded listener is here: http://codepen.io/brenzy/pen/VLYwGN 您的Codepen与TilesLoaded侦听器的分支位于: http ://codepen.io/brenzy/pen/VLYwGN

Because SO needs some code: 因为SO需要一些代码:

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

On my machine, listening for both tilesloaded and idle appear to function the same. 在我的机器上,监听tileload和空闲状态似乎都起作用。 (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. 当您进行重构时,因此在侦听器触发之前打开了信息窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM