TypeError:无法读取.addTo(this.map)中未定义的属性'map'

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

我正在我的角度应用程序中使用mapbox。我正在尝试在地图上放置标记。我遵循了mapbox示例,并将其更改为angular

geojson.features.forEach(function (marker) {

      //   // create a HTML element for each feature
      var el = document.createElement('div');
      if (marker.properties.on_ground == true)
        el.className = 'marker-on_ground';
      else
        el.className = 'marker-air';

      // make a marker for each feature and add to the map
      new mapboxgl.Marker(el)
        .setLngLat(marker.geometry.coordinates)
        .addTo(this.map);

    });

但是当我保存它时,我得到一个错误TypeError:无法读取.addTo(this.map)中未定义的属性'map'

angular mapbox-gl
1个回答
0
投票

我按照@ Ashish Rajan的建议使用Arrow函数解决了该错误。我将代码更改为休假

geojson.features.forEach((marker) => {

  //   // create a HTML element for each feature
  var el = document.createElement('div');
  if (marker.properties.on_ground == true)
    el.className = 'marker-on_ground';
  else
    el.className = 'marker-air';

  // make a marker for each feature and add to the map
  new mapboxgl.Marker(el)
    .setLngLat(marker.geometry.coordinates)
    .addTo(this.map);

});
© www.soinside.com 2019 - 2024. All rights reserved.