3

I am facing some problem in displaying tooltip on each google map marker. But it only shows tooltip on only one marker. Here is my js fiddle http://jsfiddle.net/amit4mins/6UnTr/35/

$(document).ready(function(){
    var locations=[
        ['loc1',40.23, -70.34],
        ['loc7',40.23, -70.34],
        ['loc8',40.23, -70.34],
        ['loc2',41.24, -71.35],
        ['loc3',42.25, -72.36],
        ['loc4',43.26, -73.37],
        ['loc5',44.27, -74.34],
        ['loc6',45.28, -75.34],
        ['loc6',46.29, -76.34]];
function initialize() {
    console.log(locations);
    var mapOptions = {
        center: new google.maps.LatLng(-34.397, 150.644),
        zoom: 3,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);

    var marker, i;

    for (i = 0; i < locations.length; i++) {

        var infoWindow = new google.maps.InfoWindow({
            content: locations[i][0],
        });
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map,
            title:locations[i][0]
        });  
        google.maps.event.addListener(marker, 'click', function() {
            infoWindow.open(map, marker);
        });

    }
    marker.setMap(map);

}
google.maps.event.addDomListener(window, 'load', initialize);    

});

Can any one let me know what i am doing wrong.

Many Thanks, M.

Amit Kumar
  • 3,384
  • 6
  • 25
  • 42

1 Answers1

3

Change your add listener code like below

google.maps.event.addListener(marker, 'click', (function(mm, tt) {
    return function() {
        infoWindow.setContent(tt);
        infoWindow.open(map, mm);
    }
})(marker, locations[i][0]));

DEMO

Ramesh Kotha
  • 8,266
  • 17
  • 66
  • 90
  • Thanks a lot @Ramesh..It works.. :) – Amit Kumar Jul 06 '14 at 10:33
  • small assistance more needed. If we have multiple location on same latitude and longitude. Can we display all these location name in single tooltip. Please see my updated fiddle avove – Amit Kumar Jul 07 '14 at 09:23