Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass data with marker in leaflet js

I am using leaflet js with openstreetmap in my project.
I have multiple circlemarkers at same place in my map.
I want to store some Id in that circlemarkers so that I can Identify that which data should be refereed when circlemarker is clicked.

My circlemarker is

var myMarker = L.circleMarker(myPoint, { title: 'unselected', radius: 20 }); myMarker.addTo(map);  

Here I am using title for other purpose that's why I cant use it.
Can any one tell me some way to do this.

like image 650
vaibhav shah Avatar asked Jul 02 '13 10:07

vaibhav shah


People also ask

How do you add a marker in leaflet?

Adding a Simple MarkerStep 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.

How many markers can leaflet handle?

The Clusterer can handle 10,000 or even 50,000 markers (in chrome).

What is leaflet FeatureGroup?

Extended LayerGroup that makes it easier to do the same thing to all its member layers: bindPopup binds a popup to all of the layers at once (likewise with bindTooltip ) Events are propagated to the FeatureGroup , so if the group has an event handler, it will handle events from any of the layers.


1 Answers

It sounds like you would like to add new functionality (functions, properties, etc) to an existing class. It would make sense to use object-oriented principals for this. For this purpose, I'd recommend you extending the CircleMarker class to add those properties.

customCircleMarker = L.CircleMarker.extend({    options: {        someCustomProperty: 'Custom data!',       anotherCustomProperty: 'More data!'    } }); 

Now when you create your circle marker, create an instance of your extended object instead.

var myMarker = new customCircleMarker(myPoint, {      title: 'unselected',     radius: 20,     someCustomProperty: 'Adding custom data to this marker!',     anotherCustomProperty: 'More custom data to this marker!' }); myMarker.addTo(map); 

Now you can get the properties like you would any other option from the marker. This is just a simple case of extending, and you can do more as needed, such as adding other properties or functions to the object.

JSFiddle example: JSFiddle

like image 142
Patrick D Avatar answered Sep 28 '22 07:09

Patrick D