TypeError:this.getISO不是一个函数

时间:2019-09-02 07:49:39

标签: reactjs

我遇到类型错误:this.getISO不是一个函数。

我正在尝试使用3个等时线图来实现mapbox API。但是我收到一个错误,说this.getISO不是一个函数。

map.on("load", function() {
      map.addSource("iso", {
        type: "geojson",
        data: {
          type: "FeatureCollection",
          features: []
        }
      });

      map.addLayer(
        {
          id: "isoLayer",
          type: "fill",
          // Use "iso" as the data source for this layer
          source: "iso",
          layout: {},
          paint: {
            // The fill color for the layer is set to a light purple
            "fill-color": "#5a3fc0",
            "fill-opacity": 0.3
          }
        },
        "poi-label"
      );
      this.getISO(data => {
        map.getSource("iso").setData(data);
      });
    });
    this.map = map;
  }

  getISO(callback) {
    const { duration, lng, lat } = this.state;
    fetch(
      `https://api.mapbox.com/isochrone/v1/mapbox/driving/${lng},${lat}?
        contours_minutes=${duration}&polygons=true&
        access_token=pk.eyJ1Ijoiam9uYnN0b3JleSIsImEiOiJjanl5aGFyaWwxaGE3M21ycnhsNGpvYmk2In0.jfxHlg5boDqdiUgf3cco2A`
    )
      .then(response => response.json())
      .then(data => {
        callback(data);
      });
  }

2 个答案:

答案 0 :(得分:1)

在第一行用箭头功能替换功能定义,并且

map.on("load", ()=> {

答案 1 :(得分:1)

使用箭头功能() => {代替function() {