user525146
user525146

Reputation: 3998

alert the marker value in google maps

I am trying to alert the value in the marker so that I can submit a form and pass the value to my servlet to do some processing. I am new to google maps, i am so confused to get this working. infoWindow works fine, when I hover over, it shows the location name and little description and when i mouseout the infowindow closes. I am not able to alert the clicked marker value. Can anyone guide me.

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

            marker = new google.maps.Marker({
                position: new google.maps.LatLng(location[i][2], location[i][3]),
                map: map
            });

            google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
                var content = contentString + location[i][0] + " - " +  location[i][1] + "</DIV>";
                return function() {
                    infowindow.setContent(content);
                    infowindow.open(map, marker);
                }
            })(marker, i));

            google.maps.event.addListener(marker, 'mouseout', function() {
                infowindow.close();
            });


            google.maps.event.addListener(marker, 'click', function() {

            alert(location[i][0].trim());

            });

        }

Upvotes: 0

Views: 1435

Answers (1)

Anto Jurković
Anto Jurković

Reputation: 11258

You can solve in similar way as for mouseover event listener:

    (function(i) {
        google.maps.event.addListener(marker, 'click', function() {
            alert(location[i][0].trim());
        });
    })(i);

Otherwise error

Uncaught TypeError: Cannot read property '0' of undefined 

is reported because i is set past last entry of location array.

Upvotes: 1

Related Questions