首页 > 解决方案 > 类型错误:this.getISO 不是函数

问题描述

我收到类型错误: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);
      });
  }

标签: reactjs

解决方案


在第一行用箭头函数替换函数定义和

像这样map.on("load", ()=> {


推荐阅读