I have a simple Leaflet map and I am trying to display multiple point layers. I know this question has been asked quite a few times and I have tried every single solution but didn't work.
I get valid GeoJSONs (valid because I can display them individually) from two URLs. I can load the layers in my map and the map.hasLayer()
returns true for both layers. But the map only displays the last layer, the first layer is not displayed.
I have tried different HTTP calls using Axios, jQuery, and plain XMLHTTPRequest too but the same behavior.
Logging the JSONs to the console shows that one layer has 0 features even though I am using the same code to load the second layer.
layer 1 console log
Object { type: "FeatureCollection", features: [] }
features: Array []
type: "FeatureCollection"
<prototype>: Object { … }
layer 2 console log
Object { type: "FeatureCollection", features: (5427) […] }
features: Array(5427) [ {…}, {…}, {…}, … ]
type: "FeatureCollection"
<prototype>: Object { … }
Is Leaflet overwriting on the features of the first layer?
Here is my code:
BASECOORDS = [51.96236, 7.62571];
var TILE_URL = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
var MB_ATTR = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
mymap = L.map('llmap').setView(BASECOORDS, 13);
L.tileLayer(TILE_URL, {attribution: MB_ATTR}).addTo(mymap);
var ParkingLayer = null;
var TransLayer = null;
findNearby = function () {
$.getJSON('/parking',
function (geojsonPark) {
ParkingLayer = L.geoJson(geojsonPark),
console.log(geojsonPark)
ParkingLayer.addTo(mymap);
return ParkingLayer
});
};
findNearby2 = function () {
$.getJSON('/trans',
function (geojsonTrans) {
TransLayer = L.geoJson(geojsonTrans),
console.log(geojsonTrans)
TransLayer.addTo(mymap);
return TransLayer;
});
};
findNearby()
findNearby2()
findnearby2()
function beforefindnearby()
, the effect is reversed. In that case I can see the other layer and not this one.