0

When you click on some POI on the Google map an system built-in info window will be opened by the map engine. How do you get a reference to that window (I want to close it on the next click on the map).

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Kolyunya
  • 5,973
  • 7
  • 46
  • 81
  • You can't reference those. – geocodezip Jul 18 '14 at 16:04
  • Such a nice commentless down-vote and a close-vote. – Kolyunya Jul 18 '14 at 16:44
  • possible duplicate of [Work with native infoWindows of interesting places in google maps api](http://stackoverflow.com/questions/21202034/work-with-native-infowindows-of-interesting-places-in-google-maps-api/21884991#21884991) – geocodezip Jul 18 '14 at 17:26
  • possible duplicate of [How to disable infowindow from registered places from map?](http://stackoverflow.com/questions/13117218/how-to-disable-infowindow-from-registered-places-from-map/13117452#13117452) – geocodezip Jul 18 '14 at 17:27

2 Answers2

3

This is issue #4797 tracked in the Google Maps API issue tracker, and was recently fixed, and available in version 3.26.

Starting with version 3.26 you should listen to the "click" event on the Map object. If the user clicks on a POI a IconMouseEvent is raised. This class extends the Regular MouseEvent and contains a property called placeId. So you can check if the event object has defined placeId. The placeId field contains of course a Place Id, that you can use with Places API to get more info on the icon that was clicked.

I prepared a small demo: http://jsbin.com/parapex/10/edit?html,output

In short your map "click" event handler should look like this:

// 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
  }
};
Wolf Bergenheim
  • 416
  • 5
  • 7
2

There have been some similar questions before:

The approach there is to override a method of the Infowindow-prototype to get a reference, adapted code:

//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);
  };})();

Demo: http://jsfiddle.net/doktormolle/Q7Gbb/

Community
  • 1
  • 1
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201