1

I have a geojson file which has marker-color properties in it. In this case I want to call the marker color contained in the GeoJSON file, to change the marker color. how to do this?

GeoJSON data:

    {
        "type":"FeatureCollection",
        "features":[
            {
                "type":"Feature",
                "properties":{"markercolor":"#1e7e7e","marker-size":"medium","marker-symbol":"","title":"Indonesia"},
                "geometry":{"type":"Point","coordinates":[107.9296875,-6.664607562172573]}
            },
            {
                "type":"Feature",
                "properties":{},
                "geometry":{"type":"Point","coordinates":[114.873046875,4.565473550710278]}
            },

Leaflet php code:

L.geoJSON(data,{
   pointToLayer: function (feature, latlng) {
        return new L.circleMarker(latlng, {
            radius: 8,
            fillColor: "#7e7e7e",
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8,
           
        });
    },
   
    onEachFeature:onEachFeature
    }).addTo(mymap);
});
1

You have access to feature properties inside your poinToLayer function through feature parameter, so you can reference your desired marker color with feature.properties.markercolor:

L.geoJSON(data,{
 pointToLayer: function (feature, latlng) {
  return new L.circleMarker(latlng, {
    radius: 8,
    fillColor: feature.properties.markercolor,
    weight: 1,
    opacity: 1,
    fillOpacity: 0.8
  });
  },
  onEachFeature: onEachFeature
  }).addTo(mymap);
});
  • how to call marker-size from geojson file in leaflet.js? – olif Dec 9 '20 at 13:37
  • You asked about setting marker color. Did the answer helped you solving your problem? – TomazicM Dec 9 '20 at 13:55
  • thank you, the answer is very helpful – olif Dec 9 '20 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.