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 :
<!DOCTYPE html>
<html>
<head>
<title>data</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="http://maps.stamen.com/js/tile.stamen.js?v1.3.0"></script>
<script src="/js/leaflet-0.7.2/leaflet.ajax.min.js"></script>
<script src="htdocs/webmap101/leaflet.ajax.min.js" type="text/javascript" ></script>
<style>
#map {width: 960px; height:700px;}
</style>
</head>
<body>
<div id="map"></div>
<script>
// create map
var lat = 44.933331; var lon = 4.9;
var map = L.map('map',{center: [lat,lon],zoom: 6});
// basemaps
var base = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',
{attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
});
var toner = new L.StamenTileLayer("toner");
var watercolor = new L.StamenTileLayer("watercolor");
var landscape = L.tileLayer('https://tile.thunderforest.com/landscape/{z}/{x}/{y}.png');
base.addTo(map);
// markers
var mark1=L.marker([lat,lon])
mark1.addTo(map)
// layers panel
L.control.layers({"base": base, "water": watercolor, "toner": toner , "landscape" : landscape}).addTo(map);
// geojson files
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);
//ajax
var geojsonLayer = new L.GeoJSON.AJAX("foo.geojson");
geojsonLayer.addTo(map);
</script>
</body>
</html>
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"
foo.geojson
file located?