Javascript 传单:异步替代pointToLayer?
现在,我有一些类似于:Javascript 传单:异步替代pointToLayer?,javascript,promise,leaflet,Javascript,Promise,Leaflet,现在,我有一些类似于: this.Layer=传单.geoJSON(geoJSON{ pointToLayer:功能(特性、latlng){ 返回传单。标记(板条){ 图标:箭头图标, rotationAngle:(特征.属性).DirectionTo, 标题:(feature.properties).Speed.toString() }); } } 这是可行的。对于GeoJSON中的每个点特性,调用我的函数,然后创建一个自定义标记 但是,我想将其更改为pointToLayer函数是异步的;类
this.Layer=传单.geoJSON(geoJSON{
pointToLayer:功能(特性、latlng){
返回传单。标记(板条){
图标:箭头图标,
rotationAngle:(特征.属性).DirectionTo,
标题:(feature.properties).Speed.toString()
});
}
}
这是可行的。对于GeoJSON中的每个点
特性,调用我的函数,然后创建一个自定义标记
但是,我想将其更改为pointToLayer
函数是异步的;类似于:
this.Layer=传单.geoJSON(geoJSON{
pointToLayer:异步函数(特性,latlng){
var data=await retrieveSomeMetadataForFeature(特征);
返回传单。标记(板条){
图标:数据。图标,
rotationAngle:(特征.属性).DirectionTo,
标题:(feature.properties).Speed.toString()
});
}
}
不幸的是,传单似乎不支持将承诺作为返回类型处理
我似乎也不能简单地返回null
并手动添加标记,例如:
this.Layer=传单.geoJSON(geoJSON{
pointToLayer:异步函数(特性,latlng){
var data=await retrieveSomeMetadataForFeature(特征);
var标记=传单标记(板条{
图标:数据。图标,
rotationAngle:(特征.属性).DirectionTo,
标题:(feature.properties).Speed.toString()
});
}
//在此处手动将标记添加到图层
return null;//只需告诉传单这已处理
}
标记出现时,传单不会再次调用pointToLayer
问题:
- 有没有一种方法可以调用
并将结果异步返回到传单?(大概,所要完成的就是将标记添加到正确的图层组?)pointToLayer
- 若做不到这一点,我能不能告诉传单根本不要做标记,而且这类“事件”已经处理好了
- 或者,是否有替代
的方法覆盖标记创建pointToLayer
图层组
逐点特征,然后用所需的标记填充所述的图层组
,例如
L.geoJSON(geoJson, {
pointToLayer: function (feature, latlng) {
var emptyGroup = L.layerGroup();
retrieveSomeMetadataForFeature(feature).then(function(metadata){
L.marker(icon: metadata.icon).addTo(emptyGroup);
});
return emptyGroup;
}
});
这样,pointToLayer
回调将始终返回L.Layer
的一个实例,正如L.GeoJSON
所期望的那样。L.GeoJSON
实例将包含L.LayerGroup
的多个实例,每个实例都包含L.Marker
的一个实例,但开销不应大到足以成为一个c是的!我想这就是我要找的。让我试试。