I'm quite new to JavaScript and I have been trying to add my GeoJSON files to a web page. I can't seem to figure out how to get the GeoJSON to actually load. I've checked it using GeoJson.io and it draws correctly there. I just can't get it to load on my page. The OSM portion works just fine no issues showing that.
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(mymap);
var StratStyle = {
"color": "#d3d3d3",
"weight": 5,
"opacity": 0.65
};
var geojson = new L.GeoJSON.AJAX("StratfordBoundary", {style:StratStyle});
geojson.addTo(map);
here is the entire code
Best Answer
New answer:
Try this:
The errors I could find:
L.GeoJson.ajax
needs to beL.GeoJSON.AJAX
var mymap = L.map('map1').setView([43.370917,-80.983042], 13);
needs to be in your script, not inside your style definitiondiv container
for your map was missingHowever, the .html file above now works fine for me with my own test .geojson. I just drew a simple polygon in QGIS and saved it as StratfordBoundary.geojson (EPSG:4326) in the same folder as the .html file.
Old answer:
Try without a style first until it works:
Add the filetype and check if the path to your .geojson is correct. Also make sure to point to the correct map instance you are using. I assume it is
mymap
as you use this for OSM and this one is working. If its working you can try:It also seems like you are pointing to a wrong path for the plugin. It should be without a
/
at the beginning:Alternatively you can also use jQuery with
$.getJSON
. Here is an example:Also try the console of your webbrowser. This may help finding your issue. Firefox and Chrome