如何解决mapbox gl js上的addSource错误

问题描述 投票:0回答:1

我正在使用mapbox gl js库从本地文件加载本地geojson文件。当我尝试在地图上加载它时,显示错误“ 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
            }
        });
    });
    }
angular maps mapbox geojson mapbox-gl-js
1个回答
1
投票

您的问题是JavaScript在函数中重新定义“ this”的方式。

最简单的解决方案是使用箭头功能而不是常规功能:

this.map.on('load', () => {
© www.soinside.com 2019 - 2024. All rights reserved.