javascript - Leaflet: How to style 2000+ points in a GeoJSON object with no style properties? -
i have single geojson featurecollection object contains on 2000 features. in geojson object, each feature part of 1 category so:
{ "type": "feature", "properties": { "category": "electrical", "name": "plane no 2" }, "geometry": { "type": "point", "coordinates": [ 94.5703125, 58.722598828043374 ] } }, { "type": "feature", "properties": { "category": "military", "name": "base 1" }, "geometry": { "type": "point", "coordinates": [ 104.4140625, 62.91523303947614 ] } },
in actual data, there total of 38 categories (each feature assigned 1 category).
is using javascript switch statement in situation practical solution in order give each point own styling? or, there better way?
i doing in code:
l.geojson(mygeojson, { oneachfeature: function (feature, layer){ layer.bindpopup(l.util.template(popuptemplate, feature.properties)); }, pointtolayer: function (feature, latlng){ return l.circlemarker(latlng, gjsonoptions); }, // 3 of 38 categories listed here example style: function(feature){ switch(feature.properties.category){ case 'electrical': return { color: '#fb8072'}; case 'military': return { color: '#b3de69'}; case 'aviation': return { color: '#80b1d3'}; } } }).addto(map);
i think 1 should add colors on clientside, he/she did in code example. adding colors each geojson feature needlessly bloat transfer. if want add them collection create sort of legend property in collection object so:
var collection = { "type": "featurecollection", "properties": { "legend": { "electrical": "#fb8072", "military": "#b3de69", "aviation": "#80b1d3" } } "features": [...] }
so when create geojson layer can add them on fly:
l.geojson(collection, { 'style': function (feature) { return { 'color': collection.properties.legend[feature.properties.category] } } }).addto(map);
you instead of storing legend in collection object, store in code/script somewhere:
var legend = { "electrical": "#fb8072", "military": "#b3de69", "aviation": "#80b1d3" } l.geojson(collection, { 'style': function (feature) { return { 'color': legend[feature.properties.category] } } }).addto(map);
edit after comments:
if need set l.marker icons should use pointtolayer function:
l.geojson(collection, { 'pointtolayer': function (feature, latlng) { return new l.marker(latlng, { 'icon': new l.icon({ 'iconurl': 'icons/' + feature.properties.category + '.png' ... }) }) } }).addto(map);
you're using l.circlemarker doesn't support icon option. it's path supports pathoptions:
here's nice tutorial on creating l.marker's custom icons:
Comments
Post a Comment