1

I'm trying to my geojson feature 'type_id' to add my json data to different layers.

OnEachPlant = (latlngTemp) ->
  switch ''.feature.properties.type_id
    when '2'
      myIcon = L.divIcon(
        className: 'sIcon'
        html: 'S' )
      shrubMarkers.addLayer L.marker(latlngTemp, {icon: myIcon})
    when '3'
      myIcon = L.divIcon(
        className: 'tIcon'
        html: 'T' )
      treeMarkers.addLayer L.marker(latlngTemp, {icon: myIcon})

$.ajax dataType: 'text' url: 'map.json' success: (data) -> L.geoJSON(JSON.parse(data), pointToLayer: (feature, latlng) -> OnEachPlant(latlng))

2

If I'm understanding correctly, you are running OnEachPlant() as the pointToLayer() method.

Let me quote from the Leaflet docs:

pointToLayer: A Function defining how GeoJSON points spawn Leaflet layers. It is internally called when data is added, passing the GeoJSON point feature and its LatLng. The default is to spawn a default Marker:

function(geoJsonPoint, latlng) {
    return L.marker(latlng);
}

So, if you define a pointToLayer function, such a function must receive two arguments, and must return an instance of L.Layer (a L.Marker, a L.Circle, or whatever). Furthermore, this is explained in the Leaflet GeoJSON tutorial.

I guess what you want to do is along the lines of:

function plantToMarker(feat, latlng) {
  var icon:
  switch (feat.properties.type_id) {
    case 2:
    case '2':
      icon = L.divIcon({html: 'S'});
      break;
    case 3:
    case '3':
      icon = L.divIcon({html: 'T'});
      break;
    // rest of case statements for the switch go here
  }
  return L.marker(latlng, icon);
}

$.ajax({
  url: whatever,
  success: (data)-> {
    L.geoJSON(data, {
      pointToLayer: plantToMarker
    })
  }
})

Note the use or return L.marker() in the function, and how the function definition is passed directly as an option for the L.GeoJSON factory.

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.