Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple markers with infowindows (Google Maps API)

I'm currently using the following code the place multiple markers on a Google Map using their API.

The problem I'm having is with multiple infowindows not working (only showing the last one).

There are loads of questions like mine here on SO. Actually make that a shitload of questions :-)

Just an example: Trying to bind multiple InfoWindows to multiple Markers on a Google Map and failing

The solution to my problem is kinda easy: just enclose the click listener to a (anonymous) function.

However what I don't understand is why my solution isn't working (saving the markers and infowindows in arrays instead of just one variable).

    var markers = [];     var infowindows = [];      // add shops or malls     for (var key in data.markers) {       if (data.markers.hasOwnProperty(key)) {         infowindows[key] = new google.maps.InfoWindow({             content: data.markers[key].infowindow         });          markers[key] = new google.maps.Marker({                 position: new google.maps.LatLng(data.markers[key].location.lat, data.markers[key].location.lng),                 map: map,                 flat: true,                 title: data.markers[key].name,                 draggable: false         });         var iconFile = 'http://maps.google.com/mapfiles/ms/icons/'+marker_color+'-dot.png';         markers[key].setIcon(iconFile);          google.maps.event.addListener(markers[key], 'click', function() {           infowindows[key].open(map, markers[key]);         });       }     } 

So... I don't what to get the solution how to get it working with some function to enclose the listener (although it should work, haven't tested it yet but will), but I want to know the reason why it wouldn't work if I add the markers and infowindows to arrays instead.

like image 268
PeeHaa Avatar asked Aug 12 '11 18:08

PeeHaa


People also ask

How do I add multiple markers to Google Maps API?

Follow the below steps and add/show multiple markers on google maps using javascript with infowindows: Step 1 – Create HTML File For Display Multiple Markers. Step 2 – Add Google Maps API V3 in HTML. Step 3 – Implement JavaScript Function To Create Markers/Pins And Show on Google Map.

How do I show multiple Infowindows on Google Maps?

This was how I approached the problem, with the relevant sample code. //create a marker object first, or instantiate it by passing a JSON to the constructor. //this can be done inside a for loop var infowindow = new google. maps. InfoWindow({ //add relevant data here }); //creates an infowindow 'key' in the marker.

How many markers can Google Maps API handle?

2048 characters in URL is just under 100 GeoCode values. So, again no more than 100 markers.


2 Answers

Javascript has a language structure called "closures". Closures are functions (such as the function() {} you declare above to deal with click listener) which capture references to external variables.

There are plenty of resources which explain them better than I can, which I suggest you consult, but here's my best attempt:

In this block here:

    google.maps.event.addListener(markers[key], 'click', function() {       infowindows[key].open(map, markers[key]);     }); 

Because "key" is already defined as an external variable, the function will capture a reference to that variable. So where you expect:

infowindows["helloworld"] 

Javascript will instead interpret this as:

infowindows[reference to key] 

When you click on a marker, it looks up "reference to key" to see what the current value of key is. Because this probably won't happen until your loop has finished, key will be equal to whatever the last key in your data.markers object is. And it will be equal to that value for EVERY click listener you added.

The solution, as you point out, is to wrap this in an anonymous function to get Javascript to evaluate the value of "key" at the time that the click listener is added.

  google.maps.event.addListener(markers[key], 'click', function(innerKey) {       return function() {           infowindows[innerKey].open(map, markers[innerKey]);       }     }(key)); 
like image 145
plexer Avatar answered Sep 23 '22 02:09

plexer


This works fine for me! Just add a new property to marker object, this property contains the infowindow object.

var mytext = 'Infowindow contents in HTML' var myinfowindow = new google.maps.InfoWindow({     content: mytext });  var marker = new google.maps.Marker({     position: mypos,     map: mymap,     icon: myicon,     title: mytitle,     infowindow: myinfowindow });  google.maps.event.addListener(marker, 'click', function() {         this.infowindow.open(map, this);  }); 
like image 23
Tekbreak Avatar answered Sep 27 '22 02:09

Tekbreak