1
votes

I use Leaflet for mapping solution for a proof of concept work, and currently stuck for the last couple days at the following issue:

I am adding a marker by clicking on Leaflet map, and by using following leaflet function:

alert(JSON.stringify(e.layer.toGeoJSON()));

I get this GeoJSON string result:

{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[115.1806640625,-2.7235830833483856]}}

What I need is the following GeoJSON string:

{"type":"Feature","properties":{"namo":"Babakansumedang"},"geometry":{"type":"Point","coordinates":[115.1806640625,-2.7235830833483856]}}

The question: How can I programmatically using Leaflet function/Javascript or other possible way, inserting "namo":"Babakansumedang" into GeoJSON "properties" ?

1

1 Answers

1
votes

You can pass that alert to variable and then create a new key value pair for each feature.

var json = JSON.stringify(e.layer.toGeoJSON()),
    features = json.features;

features.forEach(feat) {
  json.feat.properties["namo"] = "Babakansumedang";
}