S. Hadi
S. Hadi

Reputation: 65

Programmatically provide data property for a marker on a Leaflet map

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" ?

Upvotes: 1

Views: 1382

Answers (1)

geraldarthur
geraldarthur

Reputation: 1524

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";
}

Upvotes: 1

Related Questions