Ionic2 Leaflet marker onClick event with geoJson

Hi,

I am getting an error when adding onClick event to a marker, generated by GeoJSON. I have tried using the following: layer.on(‘click’, (e)=> {this.onMapClick(e)});

The error I receive when clicked is ERROR TypeError: _this.onMapClick is not a function


onMapClick(e) {
    console.log(e.latlng.lng, e.latlng.lat);
}

initMap(){
      this.MapData.getPoints().subscribe((res) => {
             
                this.markers = res;

                   function oneachFeature( feature, layer){
                      layer.bindPopup(feature.properties.description);
                      layer.on('click', (e)=> {this.onMapClick(e)});
                      
                     }
                           var myLayer = L.geoJSON(this.markers, {
                           pointToLayer: function(feature, latlng) {
                             if(feature.properties.route === "red"){
                             var smallIcon = new L.Icon({
                                 iconSize: [27, 27],
                                 iconAnchor: [13, 27],
                                 popupAnchor:  [1, -24],
                                 iconUrl: 'assets/images/redx2.png'
                             });
                             return L.marker(latlng, {icon: smallIcon});
                           } else if (feature.properties.route === "blue" ){
                              var smallIcon = new L.Icon({
                                 iconSize: [27, 27],
                                 iconAnchor: [13, 27],
                                 popupAnchor:  [1, -24],
                                 iconUrl: 'assets/images/bluex2.png'
                             });
                             
                            return L.marker(latlng, {icon: smallIcon});
                        }},
                           onEachFeature: oneachFeature
                       })

                        myLayer.addTo(this.map);
              });
}

Resolved: needed tochange to this:

onEachFeature: oneachFeature.bind(this)

1 Like