3
我正在使用一些區域的小冊子和幾何圖形按照顏色顯示有關這些區域在地圖上的相關計算。 我用一個稱爲calculation()
的函數來計算每個區域值。這個函數的輸出是一個簡單的浮點數。這是我所做的:設置每個小冊子功能的樣式
- 我使用
onEachFeature
加載每個功能。 - 然後我使用Ajax做一些計算。 (我在這裏使用Jquery)
- 我將計算結果保存在一個數組中。
- 然後,我用
.done
來設置每個特徵(這是一個區域)的風格來設置它的顏色。
這裏是我的代碼:
var region=L.geoJSON(qom, {onEachFeature:
function (feature, layer) {
$.ajax({ //send the ajax with following parameter
type:"POST",
timeout: 2000,
cache:false,
url: "../tool/calculation.php", //the php we are sending info too and has got
data: { }, // multiple data sent using ajax
success: function (data) { //now we have data
$.each(data, function(calculation_related_key,calculation_related_content) {
region_array_calculation.push([the_region_id, calculation_related_content['result']]);
});
}
}).done(function(){
L.geoJSON(qom, {style : set_feature_style(feature, region_array_calculation)}).addTo(mymap);
});
}
});
這裏是設置樣式代碼:
function set_feature_style (feature, calculation_array) {
for (i=0;i<calculation_array.length;i++) {
if (calculation_array[i][0]==feature.properties.region_id) {
return {
weight: 0.5,
opacity: 0.3,
color: 'Black',
dashArray: '3',
fillOpacity: 0.2,
fillColor:get_feature_color(calculation_array[i][1])
}
}
}
}
這裏我設置的顏色
function get_feature_color(input) {
var x= input;
switch (true) {
case (x>=0 && x<=0.5):
return 'blue';
break;
case (x>0.5 && x<=1):
return 'green';
}
}
但不幸的是,結果我得到的是全部用藍色。然而,一些地區的計算結果大於0.5。
感謝您的回答。其實,我正在使用傳單,而且qom是一個將被加載的geojson。我需要使用傳單API中的樣式。問題是,當我運行上面的代碼時,樣式不會影響每個功能。所有地區的顏色都會改變。我不知道這裏有什麼問題。 – keloniton