簡體   English   中英

如何從 GeoJSON 數據中設置自定義標記圖標

[英]How to set custom marker icons from GeoJSON data

我收到來自服務器的 GeoJSON 格式的響應,並將其全部添加到 map

$.ajax({
        url: '/coupling/select/',
        type: 'post',
        data: {
            sw_lat: southWest.lat,
            sw_lon: southWest.lng,
            ne_lat: northEast.lat,
            ne_lon: northEast.lng
        },
        success: function (data) {
            L.geoJSON(data, {
                icon: leafletIcon, 
                onEachFeature: onEachFeature
            }).addTo(map)
        },
        complete: function () {
            loader_countdown_dec('coupling');
        }
    }); 

代碼 function 傳單圖標:

function leafletIcon(feature){
    if(feature.properties.obj == 1){
        return L.icon({iconUrl: 'images/cable.png'});
    }
    else if(feature.properties.obj == 2){
        return L.icon({iconUrl: 'images/boxpon.png'});
    }
    else if(feature.properties.obj == 3){
        return L.icon({iconUrl: 'images/coupling.png'});
    }
}

我不知道如何根據 object 編號更改圖標。 感謝幫助!

您必須將圖標直接添加到標記:

 L.geoJSON(data, {
                pointToLayer: pointToLayer, 
                onEachFeature: onEachFeature
            }).addTo(map)

function pointToLayer(feature, latlng) {
    return L.marker(latlng, {icon: leafletIcon(feature)});
}

暫無
暫無

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

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