Reputation: 135
I want to visualize the following GeoJSON-Object that is sent to my Leaflet/JavaScript application. How can I visualize all the linestrings, showing the properties.name parameter for each linestring (e.g., by ToolTip).
{"features":[{"type":"Feature","properties":{"name":"0"},"geometry":{"type":"LineString","coordinates":[[10.941445541381836,52.438697814941406],[10.941454124450684,52.4387092590332],[10.941451263427734,52.43870544433594],[10.941445541381836,52.438697814941406],[10.941254806518555,52.440738677978516]]}},{"type":"Feature","properties":{"name":"0"},"geometry":{"type":"LineString","coordinates":[[10.941445541381836,52.438697814941406],[10.941454124450684,52.4387092590332],[10.941451263427734,52.43870544433594],[10.941445541381836,52.438697814941406],[10.941254806518555,52.440738677978516]]}}}
Any help would be great!
Thanks
Upvotes: 0
Views: 1037
Reputation: 1785
You're not using GeoJSON correctly. If you have multiple feature you should use a feature collection : https://geojson.org/geojson-spec.html
Then use this website to have the correct format : https://jsonformatter.curiousconcept.com/
Then if you want to add tooltip refer to this tutorial : http://leafletjs.com/examples/geojson/
var geojsonFeature = {
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":{
"type":"LineString",
"coordinates":[
[
10.941445541381836,
52.438697814941406
],
[
10.941454124450684,
52.4387092590332
],
[
10.941451263427734,
52.43870544433594
],
[
10.941445541381836,
52.438697814941406
],
[
10.941254806518555,
52.440738677978516
]
]
},
"properties":{
"name":"0"
}
},
{
"type":"Feature",
"geometry":{
"type":"LineString",
"coordinates":[
[
10.941445541381836,
52.438697814941406
],
[
10.941454124450684,
52.4387092590332
],
[
10.941451263427734,
52.43870544433594
],
[
10.941445541381836,
52.438697814941406
],
[
10.941254806518555,
52.440738677978516
]
]
},
"properties":{
"name":"0"
}
}
]
};
function onEachFeature(feature, layer) {
// This is where it loop through your features
if (feature.properties) {
// If there is a properties document we bind a tooltip with your property : name
layer.bindTooltip(feature.properties.name);
}
}
L.geoJSON(geojsonFeature, {
onEachFeature: onEachFeature
}).addTo(map);
Upvotes: 1