The answers I have tried prior to asking this question:
Loading external GeoJSON file into Leaflet map?
Load external geojson file into leaflet map
Issue in loading external geojson file into leaflet map?
GeoJSON does not appear on my Leaflet map
GeoJSON not showing in leaflet map?
I have created a map using Leaflet and included GeoJSON data in the same directory as my program. I am testing this code by opening in both Firefox and Chrome, both show my map in the proper location, neither show the geometry of my features.
I am using a local file which can be seen in my script. Below is the relevant includes and code:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/mapinfo.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<link rel="stylesheet"
href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
<title>GeoArchive</title>
</head>
Code:
<div class="col-sm-8 text-left">
<h1>GeoHistory Archive</h1>
<p>This is a guide to all historical events which took place in your selected area. Select a point on
the map to see if any events took place there!</p>
<div id="mapid"
style="width: 8000px; height: 800px; display: block; margin-left: auto; margin-right: auto; width: 100%;">
</div>
<script>
var map = L.map('mapid').setView([37.7688150141044, -122.44451522827147], 14);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
map.panTo(L.latLng(position.coords.latitude, position.coords.longitude))
})
}
L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=vpKwi7e2rRjEWHBci22Y', {
attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>',
}).addTo(map);
var geojson = new L.GeoJSON.AJAX("SFLandmarks.geojson");
geojson.on('data:loaded', function () {
geojson.addTo(map);
});
</script>
<h1>
{{ search }}
</h1>
<p>{{ output }}</p>
<hr>
<h3>All Resources Attributed to Wikipedia & SFData</h3>
<p></p>
</div>
I am attempting to use AJAX to load my GeoJSON layer onto 'map' but have continuously had no luck over the last week.
Is there anything obvious I could be doing wrong? I have seen that there may be a problem using a local file on the test server in Visual Studio Code, but I am testing via Firefox as well which has been stated does not suffer from that problem.
Below is the output given by my console upon running my code via flask which is my web framework for the overall application:
$ python main.py
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [19/Dec/2019 17:02:46] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [19/Dec/2019 17:02:46] "GET /css/mapinfo.css HTTP/1.1" 404 -
127.0.0.1 - - [19/Dec/2019 17:02:46] "GET /css/mapinfo.css HTTP/1.1" 404 -
Through suggestions in the comments below, I have narrowed down the problem using the Network Tab on the Google Chrome Developers tools (F12) to determine that my AJAX.js file is not being located on the webpage giving me the two following error codes:
ajax.js:1 Failed to load resource: the server responded with a status of 404
(NOT FOUND)
Uncaught TypeError: L.GeoJSON.AJAX is not a constructor
at (index):58
This is a locally loaded file.
.geojson
enabled/defined on your server? What do you get if you try to loadSFLandmarks.geojson
file directly in browser? – TomazicM Dec 19 '19 at 14:12SFLandmarks.geojson
. Is the request happening? Is the request to the same server, same port? Is your web server returning a 404 or something like that? – IvanSanchez Dec 19 '19 at 18:23