Get marker ID on Google maps

I want to transfer the corresponding marker identifier by clicking the marker on the google map. I am using the marker.getId () function to retrieve the marker identifier. But the token id does not match the 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 = "www.cickstart.com/" + marker.getId(); }); } 
+4
source share
2 answers

you can try to directly access id:

 google.maps.event.addListener(marker, 'click', function() { window.location = "www.cickstart.com/" + marker.id; }); 
+8
source

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

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

if you have so many markers, click the marker on the array marker. You can add any data you want. and just name it, install it, or get it.

sample like this:

 markers[0].metadata.id 
+9
source

Source: https://habr.com/ru/post/1397848/


All Articles