簡體   English   中英

Leaflet:pointToLayer 的異步替代方案?

[英]Leaflet: async alternative to pointToLayer?

現在,我有類似的東西:

this.Layer = Leaflet.geoJSON(geoJson, {
    pointToLayer: function (feature, latlng) {
        return Leaflet.marker(latlng, {
            icon: arrowIcon,
            rotationAngle: (<MyFeatureType>feature.properties).DirectionTo,
            title: (<MyFeatureType>feature.properties).Speed.toString()
            });
        }
   }

這行得通。 對於 GeoJSON 中的每個Point特征,我的 function 都會被調用,我可以創建一個自定義標記。

但是,我想更改它,例如pointToLayer function 是異步的; 就像是:

this.Layer = Leaflet.geoJSON(geoJson, {
    pointToLayer: async function (feature, latlng) {
        var data = await retrieveSomeMetadataForFeature(feature);

        return Leaflet.marker(latlng, {
            icon: data.Icon,
            rotationAngle: (<MyFeatureType>feature.properties).DirectionTo,
            title: (<MyFeatureType>feature.properties).Speed.toString()
            });
        }
   }

不幸的是, Leaflet 似乎不支持將 Promise 作為返回類型處理。

看來,我不能簡單地返回null並手動添加標記,例如:

this.Layer = Leaflet.geoJSON(geoJson, {
    pointToLayer: async function (feature, latlng) {
        var data = await retrieveSomeMetadataForFeature(feature);

        var marker = Leaflet.marker(latlng, {
            icon: data.Icon,
            rotationAngle: (<MyFeatureType>feature.properties).DirectionTo,
            title: (<MyFeatureType>feature.properties).Speed.toString()
            });
        }

        // manually add marker to layer here

        return null; // simply tell Leaflet that this has been handled
   }

當標記出現時,Leaflet 永遠不會第二次調用pointToLayer

問題:

  • 有沒有辦法調用pointToLayer並將結果異步返回給Leaflet? (大概,所有能完成的就是將標記添加到正確的圖層組?)
  • 如果做不到這一點,我能否以某種方式告訴 Leaflet 根本不要創建標記,並且已經處理了各種“事件”?
  • 或者,是否有替代標記創建的pointToLayer替代方法?

每個點特征返回一個空的LayerGroup ,然后用你需要的任何Marker填充所述LayerGroup ,例如

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實例,但開銷不應大到足以成為問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM