简体   繁体   中英

How do i get the name or identification of a marker JVectorMap

How do I know the name or id of the marker that is clicked?

And is it possible?

Is there a way to find the label/name of the marker like this for example:

onMarkerClick: function(label){
    alert(label.text());
}

All help appreciated :)

The second parameter in the click handler function is the key/index of the marker object.

onMarkerClick (Event e, String code)

One possible way to do that, is:

onMarkerClick: function(e, code) {
    var mapObj = $("#map").vectorMap("get", "mapObject");
    var idx = parseInt(code); // optional
    var name = mapObj.markers[idx].config.name;
    var latitude = mapObj.markers[idx].config.latLng[0];
    var longitude = mapObj.markers[idx].config.latLng[1];
}

Explanation:

  • depending how you created the markers, you may need to get the index as numeric value
  • by default, each marker is storing the name and the coords of the point

If you need more info, try to explore the properties of the map object in the browser, like this: console.log(mapObj.markers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM