I am trying to add a .json
vector layer to a Leaflet.js map, viewable here via GitHub page, with source code here.
Here's a shortened version of the file for reference (the GitHub page linked above shows it working in context).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- LEAFLET-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>
<style>
html,
body {
height: 100%;
margin: 0;
}
#map {
width: 600px;
height: 600px;
}
</style>
</head>
<body>
<div id='map'></div>
<script>
var map = L.map('map').setView([-7.08, -78.5565467], 12);
let osmLayer = L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}@2x.png', {
attribution: 'Wikimedia maps beta | © <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
let marker = L.marker([-7.2861634, -78.579712])
.addTo(map);
let marker2 = L.marker([-7.1605494, -78.5392218])
.addTo(map);
///let test_L = L.geoJSON("test.json", {
///weight: 2,
/// color: '#100'
///}).addTo(map);
//let layerGroup = L.geoJSON(watersheds, {
// onEachFeature: function (feature, layer) {
//layer.bindPopup('</h1><p>name: '+feature.properties.WTSHNAME+'</p>');
// }
// }).addTo(map);
//street layer using mapbox basic
// Changed to OSM just to avoid the token requirement, for the purpose of demo.
var mapboxbasic = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
maxZoom: 18,
zIndex: 1
});
//aerial layer using mapbox satellite
// Changed to OSM just to avoid the token requirement, for the purpose of demo.
var mapboxsat = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
zIndex: 2
});
var Esri_WorldImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
});
var basemap = {
'Streets': mapboxbasic,
'Aerials': mapboxsat,
'ESRI Imagery': Esri_WorldImagery
} // Refer to the individual Tile Layers only. They will go into the tilePane.
;
L.control.layers(basemap).addTo(map); // Layers Control just to switch between the 2 basemaps.
</script>
</body>
</html>
The code (with the errant sections commented out) successfully add in markers using lat long coordinates, but my code breaks when I try to reference the .json
file. [By 'breaks', I mean that the base layer selection panel in the top right corner disappears and the vector layer is not added.]
The .json
file is called in using <script src="test.json" type="text/javascript"></script>
[with the .json
file hosted on GitHub]
The code I have tried is:
let test_L = L.geoJSON(test, {
weight: 2,
color: '#100'
}).addTo(map);
Following the official example at: https://leafletjs.com/examples/geojson/
And :
$.getJSON("test.json",function(data){
L.geoJson(data).addTo(newMap);
});
Following the discussions here on StackOverflow, on this forum, and as modelled with this gist -- all to no avail.
I am clearly missing something, but I don't understand what.
For comparison, I managed to get this example working on Observable.
Can anyone help to point me in the right direction? I'd really appreciate it!!
Thanks in advance for your time and guidance,
Aaron