0

Im trying to find a way to change the instance of a Leaflet geoJson Feature after it is added to the map.

This is what I want to achieve:

Importing data with L.GeoJson and I am using pointToLayer to change the marker to L.CircleMarker

Now I want

layer.on('click', function (e) {
   e.target //Do something here to change it from L.CircleMarker to L.Marker
});

Any idea how to achieve this?

Henrik Maaland
  • 210
  • 2
  • 14

1 Answers1

0
var group = L.geoJSON(); // Your geojson group on importing
layer.on('click', function (e) {
   var circlemarker = e.target //Do something here to change it from L.CircleMarker to L.Marker
   var marker = L.marker(circlemarker.getLatLng()).addTo(group);
   marker.feature = circlemarker.feature
   circlemarker.removeFrom(group)
   // Then add the same events to the layer as in pointToLayer
});
Falke Design
  • 10,635
  • 3
  • 15
  • 30
  • If i add the marker.on('click', onLayerClick); and try to fire it instantly to make user not need to do a click again marker.fire('click') It does not work as expected. If i make the user click it a second time it works as expected. Any idea why? – Henrik Maaland Dec 14 '20 at 14:48
  • No is hard to say without demo code. But why you not calling `onLayerClick({target:marker})` directly? – Falke Design Dec 14 '20 at 16:45