Monday, April 23, 2012

Have just one InfoWindow open in Google Maps API v3


I need to open only one InfoWindow on my Google Map. I need to close any other InfoWindow before i open a new one.



Can you some one guide me on this?


Source: Tips4all

7 comments:

  1. You need to create just one InfoWindow object, keep a reference to it, and reuse if for all the markers. Quoting from the Google Maps API Docs:


    If you only want one info window to display at a time (as is the behavior on Google Maps), you need only create one info window, which you can reassign to different locations or markers upon map events (such as user clicks).


    Therefore, you may simply want to create the InfoWindow object just after you initialize your map, and then handle the click event handlers of your markers as follows. Let's say you have a marker called someMarker:

    google.maps.event.addListener(someMarker, 'click', function() {
    infowindow.setContent('Hello World');
    infowindow.open(map, someMarker);
    });


    Then the InfoWindow should automatically close when you click on a new marker without having to call the close() method.

    ReplyDelete
  2. Create your infowindow out of the scope so that you can share it.

    Here is a simple example:

    var markers = [AnArrayOfMarkers];
    var infowindow = new google.maps.InfoWindow();

    for (var i = 0, marker; marker = markers[i]; i++) {
    google.maps.event.addListener(marker, 'click', function(e) {
    infowindow.setContent('Marker position: ' + this.getPosition());
    infowindow.open(map, this);
    });
    }

    ReplyDelete
  3. I had the same problem but the best answer didn't solve it completely, what I had to do in my for statement was using the this relating to my current marker. Maybe this helps someone.

    for(var i = 0; i < markers.length; i++){
    name = markers[i].getAttribute("name");
    address = markers[i].getAttribute("address");
    point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
    contentString = '<div style="font-family: Lucida Grande, Arial, sans-serif;>'+'<div><b>'+ name +'</b></div>'+'<div>'+ address +'</div>';
    marker = new google.maps.Marker({
    map: map,
    position: point,
    title: name+" "+address,
    buborek: contentString
    });
    google.maps.event.addListener(marker, 'click', function(){
    infowindow.setContent(this.buborek);
    infowindow.open(map,this);
    });
    marker.setMap(map);
    }

    ReplyDelete
  4. You'll find you answer here http://www.lootogo.com/googlemapsapi3/markerPlugin.html

    ReplyDelete
  5. You need to keep track of your previous InfoWindow object and call the close method on it when you handle the click event on a new marker.

    N.B It is not necessary to call close on the shared info window object, calling open with a different marker will automatically close the original. See Daniel's answer for details.

    ReplyDelete
  6. I think in V3 you can't open multiple infowindow in same time.

    ReplyDelete
  7. Google Maps allows you to only have one info window open. So if you open a new window, then the other one closes automatically.

    ReplyDelete