0

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 &copy; <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()
9
  • 2
    As you can see from the console log immediately after the first GeoJSON is read, GeoJSON is empty. There's obviously something wrong in the GeoJSON data.
    – TomazicM
    Aug 7 at 12:59
  • @TomazicM I thought about the same. But if I reverse the order of drawing layers, i.e. I call the findnearby2() function before findnearby(), the effect is reversed. In that case I can see the other layer and not this one.
    – akudekar
    Aug 7 at 22:48
  • And the worst part is the console shows 0 errors too.
    – akudekar
    Aug 7 at 22:52
  • What do you see in the network section of the browser debugger? What gets returned from GeoJSON retrieval calls?
    – TomazicM
    Aug 8 at 5:03
  • @TomazicM I did some more digging and the problem is, that the map is loaded before the layers can be drawn. If I refresh the page 2-3 times, the both layers are displayed. Is it possible to wait for the features to be loaded before loading the page?
    – akudekar
    Aug 8 at 9:59
0

I have 6 big GeoJSONs with 3000-4000 features each. I am running local version of PostgreSQL though. The GeoJSONs are not files, but PostGIS geometries and I convert them to GeoJSONs.

With setTimeout, I am now executing GeoJSON requests at different intervals and waiting for on request to be completed before executing the next query.

Now all the layers display correctly.

1
  • I edited answer to add info from comment.
    – TomazicM
    Aug 9 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.