2014-07-18 75 views
0

当您在Google地图上点击某个POI时,系统内置信息窗口将由地图引擎打开。 你如何获得对该窗口的引用(我想在地图上点击下一个关闭它)。如何引用系统信息窗口

+0

您不能引用的。 – geocodezip

+0

这样一个很好的评论倒票和近距离投票。 – Kolyunya

+0

可能的重复[在谷歌地图API中使用原生infoWindows中有趣的地方](http://stackoverflow.com/questions/21202034/work-with-native-infowindows-of-interesting-places-in-google-maps- api/21884991#21884991) – geocodezip

回答

2

已经有一些类似的问题之前:

的方法有覆盖信息窗口原型的方法来得到一个参考,适应代码:

//run this after loading the maps-api 

(function(){ 
    var fx = google.maps.InfoWindow.prototype.setContent; 

    //override the built-in setContent-method 
    google.maps.InfoWindow.prototype.setContent = function() { 

    //this property isn't documented, but as it seems 
    //it's only defined for InfoWindows opened on POI's 
    if (this.logAsInternal) { 
     google.maps.event.addListenerOnce(this, 'map_changed',function() { 
     var map = this.getMap(),that=this; 
     //attach the click-handler when the infowindow opens 
     if (map) { 
      google.maps.event.addListenerOnce(map, 'click', function(){that.close();}); 
     } 
     }); 
    } 
    //call the original setContent-method 
    fx.apply(this, arguments); 
    };})(); 

演示:http://jsfiddle.net/doktormolle/Q7Gbb/

3

这是在Google Maps API问题跟踪器中跟踪的问题#4797,最近已修复,并在3.26版中提供。

从版本3.26开始,您应该听取Map对象上的“click”事件。如果用户点击POI,则会引发IconMouseEvent。该类扩展了Regular MouseEvent并包含一个名为placeId的属性。所以你可以检查事件对象是否定义了placeId。 placeId字段当然包含Place ID,您可以使用Places API来获取点击图标的更多信息。

我准备了一个小的演示:http://jsbin.com/parapex/10/edit?html,output

总之你的地图“单击”事件处理程序应该是这样的:

// This is the click event handler 
var handleClick = function(event) { 
// If the event has a placeId, use it. 
    if (event.placeId) { 
    // Calling e.stop() on the event prevents the default info window 
    // from showing. 
    // If you call stop here when there is no placeId you will prevent 
    // some other map click event handlers from receiving the event. 
    event.stop(); 
    // do something with event.placeId here. Like calling places service 
    } 
};