1

I am trying to create a layer that has different types of business (separated by code properties in the geojson file) but when it executes the pointToLayer below I get an Uncaught TypeError: Cannot set property 'feature' of undefined. What am i doing wrong?

  var allbusinesses = L.geoJson(data, {
    style: allBusinessesStyle,
    onEachFeature: onEachFeature,
    pointToLayer: function(feature, latlng) {
      switch (feature.properties.code) {
        case "1":
          return L.circleMarker(latlng, {
            color: "#000"
          });
        ...
      }
    }
  });
  • You might have a bit better luck on stack overflow with this question. Can you post sample data & (even better) something like a jsfiddle? – raphael Feb 9 '16 at 22:42
  • 1
    Is there a fallback case, so that if there is no matching code, then pointToLayer will still return something? And if you strip out the switch/case statements, so that your pointToLayer function simply returns the same type of circleMarker for all layers, does it give the same error? – nathansnider Feb 9 '16 at 23:28
  • Thank you both for your help . @nathansnider putting in a default case solved the problem. If you post that as the solution i will credit you with the answer? – Eoin Lane Feb 10 '16 at 11:56
1

If any of your GeoJSON features has a code property that is not accounted for by your case statements, then your pointToLayer function will not return a feature, which can produce the error that you are seeing. Adding a default case will prevent this:

  var allbusinesses = L.geoJson(data, {
    style: allBusinessesStyle,
    onEachFeature: onEachFeature,
    pointToLayer: function(feature, latlng) {
      switch (feature.properties.code) {
        case "1":
          return L.circleMarker(latlng, {
            color: "#000"
          });

        ...

        default:
          return L.circleMarker(latlng, {
            color: "#fff"
          });
      }
    }
  });

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.