1
votes

Is it possible to define your own marker icons in GeoJSON?

I have tried many ways to get the desired effect but nothing works ..

Example code from geojson FeatureCollection where i want add custom icon:

{
    "type": "Feature",
    "id": "Point1",
    "properties": {
        "name": "Last point"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [22.57031047873893, 51.25080964529834]
    }
}
2

2 Answers

0
votes

MapBox has created a CustomMarker plugin for Leaflet that should do the trick.

And another great example from Mapbox, GeoJSON Custom Markers and Style

Here's some sample code from that site:

var geoJsonData = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": {
        "fillColor": "#eeffee",
        "fillOpacity": 0.8
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [119.2895599, 21.718679],
            [119.2895599, 25.373809],
            [122.61840, 25.37380917],
            [122.61840, 21.71867980],
            [119.2895599, 21.718679]
          ]
        ]
      }
    }, {
      "type": "Feature",
      "properties": {
        "marker-color": "#00ff00"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [120.89355, 23.68477]
      }
    }]
};

var geoJson = L.geoJson(geoJsonData, {
    pointToLayer: L.mapbox.marker.style,
    style: function(feature) { return feature.properties; }
}).addTo(map);

NOTE: This is not part of the core LeafletJS library, it requires mapbox.js (and mapbox.css)

0
votes

If you update to the latest version (at time of writing) the pointToLayer callback in your geojson object will start to work. It doesn't seem to be implemented in 0.7.7 but is currently available on master and I assume it will be in 0.8+.

If you just assign a geoJSON definition to your scope's geojson property with the function present leaflet will render it properly now.