reactjs - 类型错误: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);
});
}
解决方案
在第一行用箭头函数替换函数定义和
像这样map.on("load", ()=> {
推荐阅读
- json - 如何按属性值过滤 ARM 模板输出?
- api - 如何正确添加查询参数——颤振
- python - 将高分辨率底部地形添加到 Cartopy 地图
- cmake - Clion 失败并收到 clang:错误:链接器命令失败,退出代码为 1,但它使用 clang++ 工作
- amazon-web-services - 无法将文件从本地推送到 s3
- typescript - 可选类型别名的交集
- python - Python 中的 MQTT on_message 超时
- swift - 如何从我的项目 SwiftUI 启动谷歌地图
- wso2 - 如何在 WSO2 API Manager 中配置 Key 配置(即:Token Endpoint 和 Revoke Endpoint)
- typescript - 未生成导入的类型