Get marker id in google maps

I want to pass related marker id by clicking marker on google map. I am using marker.getId() function to retrieve marker id. But the marker id is not passing along with url. How can i do this? Any Help?

function AddressMap(lat,lang,markerid) { var latLng = new google.maps.LatLng(lat,lang); var marker = new google.maps.Marker({ 'map': map, position: latLng, 'latitude' :lat, 'longitude' :lang, icon: image, shadow: shadow, id: markerid }); markers.push(marker); google.maps.event.addListener(marker, 'click', function() { window.location = "" + marker.getId(); }); } 

2 Answers

you can try directly to access the id:

google.maps.event.addListener(marker, 'click', function() { window.location = "" + marker.id; }); 
0

the best way to do this is add a metadata to marker

var marker = new google.maps.Marker(markerOptions); marker.metadata = {type: "point", id: 1}; 

if you have so many marker push the marker to array marker. You can add any data that you want. and simply call it, set it or get it.

the sample like this one:

markers[0].metadata.id 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like