Search code examples
mapboxgeojson

Adding several geoJSON layers in Mapbox


I have two geoJSON files, one is for drawing the lines(states.js), and another one is for adding markers on the map (marker.js).

However, when I add a line of code that is supposed to be adding the marker layer, nothing works.

var markerLayer = L.mapbox.featureLayer(markers).addTo(map);

How should I approach this? I thought featureLayer would work to add multiple layers, but it doesn't seem to be working well. Help is much appreciated.

Working example so far: Plunker

Example I'm following is here.


Solution

  • Couple of things wrong here. You forgot to declare your global L.mapbox.accessToken:

        L.mapbox.accessToken = 'pk.eyJ1Ijoia2thZ2lsbCIsImEiOiJjaWdsdmJjeWkwMjMwdWFrcjI4eGZ3MGd2In0.WslWCpxaXxUOgUZP_VT1cg';
    

    You're adding the statesData twice, once in a L.mapbox.featureLayer and once in a L.GeoJSON:

    var statesLayer = L.mapbox.featureLayer(statesData).addTo(map);
    
    statesLayer = L.geoJson(statesData, {
        style: style,
        onEachFeature: onEachFeature
    }).addTo(map);
    

    Once is more than enough: here's a example on Plunker: http://plnkr.co/edit/kV8h69VJt2jtpqwdCpJo?p=preview