简体   繁体   中英

Visualizing Linestrings with properties in Leaflet

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 (eg, 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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM