Ajudoonor Ajudoonor - 11 months ago 35
Javascript Question

Is it possible to drag markers that gets it coordinates from geojson file?

It's difficult to put my question into words, but I'll try. I'm using leaflet. I have a layer that contains a lot of positions and info about streetlights, but some are bit out of position. So is it possible to drag markers that are positioned by geoJSON file info?

Bonus question (not so important at the moment): If dragging is possible, by any chance is there possibility to save those changes into geojson file also?

Code about that layer.

tv = new L.geoJson(tv, {
pointToLayer: function (feature, layer) {
return L.marker(layer, {icon: asteriskorangeicon });
onEachFeature: onEachFeature2,

function onEachFeature2(feature, layer) {
if (feature.properties) {
layer.bindPopup("<br><b><big><u>ID: " + feature.properties.id + "</br></b></big></u><br> <b>01_NR: </b>" + feature.properties.nr01
+ "<br><b>Kilp:</b> " + feature.properties.kilp + "<br><b>Phase: </b>" + feature.properties.phase_03 + "<b><br>kand_t1_04: </b>"
+ feature.properties.kand_t1_04 + "<b><br>kand_t2_05: </b>" + feature.properties.kand_t2_05 + "<br><b>kand_om_06: </b>" + feature.properties.kand_om_06
+ "<br><b>kand_kp_07:</b> " + feature.properties.kand_kp_07 + " <br><br>"


Answer Source

To make each of the markers created by L.geoJson draggable, all you need to do is add the following inside the onEachFeature function:

function onEachFeature2(feature, layer) {   
    layer.options.draggable = true;