I have two map layers that I want to show at once: One is the full data, the other is a filtered version of the data. The map will draw with each of these layers, but not with both at the same time. Any idea what is going on?

I brought in my data using:

var contour = $.getJSON("StateParks.json", function(data) {
    var myStyle = {
    "color": "#ff7800",
    "opacity": 0.65


A. Full data here:

L.geoJson(data,{ style: myStyle }).addTo(map); });

B. Filter of that data here:
L.geoJson(data,{ filter: stateFilter }).addTo(map); }); function stateFilter(feature){ if (feature.properties.State === "Wyoming"){ return true } }

In other words, A will draw, B will draw, but both A and B will not.

My StateParks.json has a format similar this:

{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[x1,y1],[x2,y2],[x3,y3]]]]},"properties":{"OBJECTID":1,"ParkArea":855,"Name":"Park Name","Picnic":"Yes","State": "Wyoming", "OfficeAddress": "Address"}}
  • 1
    Couldn't replicate this on a quick test with a different data set: fiddle.jshell.net/nathansnider/3ev404hj Could you post the whole $.getJSON callback for the non-working version with both A and B? It seems like this could be a simple fix, but it's a little hard to tell, when the code is broken up as you have it here. – nathansnider Apr 19 '16 at 21:55
  • You might like to just color Wyoming a different colour, hey? But anyhow, @nathansnider has something that works. – Alex Leith Apr 20 '16 at 0:06
  • I'm a beginner--what is a callback? The code posted above is basically my entire code, with the exception of initializing the map at the beginning. My code differs slightly from the jsfiddle example in that I uploaded data from a .json rather than a URL. What can I provide you with to help you help me? I updated info in the question showing how my json looks, if that's at all relevant. Thanks! – user2192778 Apr 20 '16 at 15:08
  • I got it to work by starting over using your jsfiddle as an example. Not sure what the problem was, but I'm happy to have made progress. Thanks again. – user2192778 Apr 20 '16 at 15:16
  • Good to hear you got it working. The callback function is just the function that is run when the getJSON request completes successfully. In your example, it's the function that starts with function(data) {. You can see the docs for more information. – nathansnider Apr 20 '16 at 22:51

Your Answer

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

Browse other questions tagged or ask your own question.