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

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

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest