maplecode
maplecode

Reputation: 151

react-leaflet <GeoJSON /> pointToLayer option to change the icon

i am currently trying to learn react and i want to use a leaflet map (react-leaflet).

I have two (Point, Polygon) GeoJson objects that i want to display, which is working, but i want to replace the default marker icon.

The leaflet documentation http://leafletjs.com/examples/geojson/ tells me to use the pointToLayer option.

Code:

onEachFeaturePoint, onEachfeaturePolygon, pointToLayer

onEachFeaturePoint(feature, layer) {
  console.log('feature: ', feature);
  console.log('layer: ', layer);
  layer.on({
    'click': function (e) {
       console.log('e: ', e);
       console.log('click');
     }
  })
}

onEachFeaturePolygon(feature, layer) {
  console.log('feature: ', feature);
  console.log('layer: ', layer);
  layer.on({
    'click': function (e) {
       console.log('e: ', e);
       console.log('click');
     }
  })
}

pointToLayer(feature, latlng) {
  console.log('--- Point to layer');
  console.log('feature: ', feature);
  console.log('latlng: ', latlng);
  return <CircleMarker center={latlng} />;
}

render

render() {
  const center = [9.4921875, 51.83577752045248];

  return (
    <Map center={center} zoom={1}>
      <GeoJSON ref='marker1' data={this.state.point} onEachFeature={this.onEachFeaturePoint.bind(this)} pointToLayer={this.pointToLayer.bind(this)} />
      <GeoJSON ref='polygon1' data={this.state.polygon} onEachFeature={this.onEachFeaturePolygon.bind(this)} />
    </Map>
  )
}

If i keep pointToLayer={this.pointToLayer.bind(this)} it stops working with the following error:

Uncaught TypeError: layer.on is not a function
    at CustomMarker.onEachFeaturePoint (MapDemo.js:73)
    at NewClass.addData (leaflet-src.js:10455)
    at NewClass.addData (leaflet-src.js:10435)
    at NewClass.initialize (leaflet-src.js:10420)
    at new NewClass (leaflet-src.js:310)
    at L.geoJSON (leaflet-src.js:10732)

I cant figure out why the error occurs, maybe someone has an idea how to solve this issue or help my understand the mistakes i made.

Edit: By replacing return <CirleMarker /> with return L.circleMarker() in the pointToLayer function, i got it to work.

Upvotes: 5

Views: 6568

Answers (2)

maplecode
maplecode

Reputation: 151

By replacing return <CirleMarker /> with return L.circleMarker() in the pointToLayer function, i got it to work.

import L from 'leaflet';

...

pointToLayer(feature, latlng) {
   return L.circleMarker(latlng, null); // Change marker to circle
   // return L.marker(latlng, { icon: {}}); // Change the icon to a custom icon
}

Upvotes: 9

Evan Siroky
Evan Siroky

Reputation: 9408

I think you might be better off trying to use the Marker component instead of the GeoJSON component if you're simply trying to display a custom icon.

<Map center={center} zoom={1}>
  <Marker
    icon={homeIcon}
    key='marker1'
    onClick={this._handleClick}
    position={leafletLatLng}
    />
</Map>

...

const homeIconUrl = 'home-2.png'
const homeIcon = icon({
  iconUrl: homeIconUrl,
  iconSize: [32, 37],
  iconAnchor: [16, 37]
})

Upvotes: 0

Related Questions