Muhammad Ahsan
Muhammad Ahsan

Reputation: 21

how to solve addSource error on mapbox gl js

i am loading local geojson file from a local file using mapbox gl js library. when i try to load this on map it gives error "addSource not defined".

 showgeojson()
    {
      var data='./assets/nyc_speed-3.geojson';
      this.map.on('load', function () {

        this.map.addSource('scmpd-precinct-polygons', {
          type: 'geojson',
          data: './assets/nyc_Speed_3.geojson'
        });

        this.map.addLayer({
            'id': 'scmpd-precinct-polygons',
            'type': 'fill',
            'source': 'scmpd-precinct-polygons',
            'layout': {},
            'paint': {
                'fill-color': '#088',
                'fill-opacity': 0.8
            }
        });
    });
    }

Upvotes: 0

Views: 1434

Answers (1)

Steve Bennett
Steve Bennett

Reputation: 126125

Your problem is the way JavaScript redefines "this" within a function.

The simplest solution is to use an arrow function instead of a regular one:

this.map.on('load', () => {

Upvotes: 1

Related Questions