I am using Leafletjs with Leaflet.draw to create an interactive map on my web.
But I need to enable different types of "marker". And then when creating them I need to identify them. I tried adding a title to each marker, but then I can't get it.
attempt 1:
https://jsfiddle.net/k9w2jf05/2/
L.DrawToolbar.include({
getModeHandlers: function (map) {
return [
{
enabled: true,
handler: new L.Draw.Marker(map, { icon: new L.Icon.Default() }),
title: 'Place restaurant marker'
},
{
enabled: true,
handler: new L.Draw.Marker(map, { icon: new L.Icon.Default() }),
title: 'Place gas station marker',
innerHTML: "sdadadds"
},
{
enabled: true,
handler: new L.Draw.Marker(map, { icon: new L.Icon.Default() }),
title: 'Place hospital marker'
}
];
}
});
attempt 2:
https://jsfiddle.net/k9w2jf05/1/
var drawPluginOptions = {
position: 'topright',
draw: {
polyline: false,
polygon: false,
circle: false, // Turns off this drawing tool
rectangle: false,
circlemarker: false,
marker: {
title: "soyel1",
icon: new MyCustomMarker()
}
},
edit: {
featureGroup: editableLayers, //REQUIRED!!
remove: false
}
};
map.on('draw:created', function(e) {
var type = e.layerType,
layer = e.layer;
/*if(layer.title == "Place hospital marker"){
do this...
}*/
editableLayers.addLayer(layer);
});
I simply need different markers to appear on the bar, with different icons and detect when one or the other is clicked, and when one or the other is deleted. To have 1 of each on the map always.
How can I do that?
Best Answer
There might be other, more 'official' ways, but the only way I could find to get some parameter from marker creation button to marker creation event, is through marker icon properties when marker is created through
handler
, defined withgetModeHandlers
option.Before marker is created with handler
new L.Draw.Marker(map, { icon: new L.Icon.Default() })
, we create icon as separate object and then any custom property can be added to it. This property can be retrieved inside the marker creation event handler withe.layer.options.icon.myType
, wheremyType
can be any name, as long it does conflict with standard icon properties.In the code below, property named
myType
is used to bind appropriate tooltip to the marker at the time of marker creation.To create marker with different icon for each button, it's easy, just create marker with the desired icon.
To change draw icon to custom icon, I tried to find official way, but got lost, so I just used some standard JS HTML CSS trickery to assign the same icons that are used for marker icons to the draw icons.