Monday, 13 July 2015

GeoJSON does not appear on my Leaflet map


I'm a beginner in Leaflet and I need help to deal with GeoJSON files.


I succeeded in creating a Leaflet basemap. I can add markers without troubles but I cannot manage to show a GeoJSON layer.



Here is the code I picked up from Leaflet Tutorials website https://leafletjs.com/examples/geojson/ :


var geojsonFeature = {
"type": "Feature",
"properties": {
"name": "Coors Field",
"amenity": "Baseball Stadium",
"popupContent": "This is where the Rockies play!"
},
"geometry": {
"type": "Point",

"coordinates": [-104.99404, 39.75621]
}
};
L.geoJSON(geojsonFeature).addTo(map);

Unfortunatly, the GeoJSON does not appear in my map.


Can you tell me where I missed something ?




Alright, you're fine Francisco, this part was OK. I changed the whole script and it works !


However, would you know how to load a GeoJSON from an external source ?



I tried this :


var geojsonLayer = new L.GeoJSON.AJAX("foo.geojson");       
geojsonLayer.addTo(map);

from Loading external GeoJSON file into Leaflet map?


but this definitely does not work.


Any ideas ?




I am not skilled enough to understand your advice.


Here is the whole code I am using to create my map :






data
















I know it's a bit messy but this is actually my very first HTML script... I guess I did not include in a proper way the AJAX library.





Actually, I just want a solution to replace the lines where I create the GeoJSON by something that tells javascript to load the GeoJSON stored locally in my folder. This :


var geojsonFeature = {
"type": "Feature",
"properties": {
"name": "Coors Field",
"amenity": "Baseball Stadium",
"popupContent": "This is where the Rockies play!"
},
"geometry": {

"type": "Point",
"coordinates": [-104.99404, 39.75621]
}
};

should be transformed into something like (I don't know if it exists) :


var geojsonFeature = [foo.geosjon]

where "foo.geosjson" is a file like :


{ "type": "FeatureCollection", "name": "abies", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } }, "features": [ { "type": "Feature"





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...