2016-11-13 62 views
0

我有一個超過11 000個多邊形的geoJSON文件。我已經爲它們中的每一個計算了一些屬性,並將其作爲特徵存儲在geoJSON中。根據計算出的屬性,是否有可能讓每個單元的不透明度發生變化?鐵。如果該屬性是1,我想的細胞幾乎透視,如果是6,我想這是幾乎固體等Google Maps API - geoJSON中每個單元的不透明度?

編輯

好了,我已經得到了實際上將不透明度值放入geoJSOn中。現在一個條目如下所示:

{'geometry': {'coordinates': [[[10.927456267537572, 45.68179119797432], 
     [10.940290010697588, 45.68157387892596], 
     [10.939979018768243, 45.67257819153854], 
     [10.927147329501077, 45.672795442796335], 
     [10.927456267537572, 45.68179119797432]]], 
    'type': 'Polygon'}, 
    'id': 1, 
    'properties': {'cellId': 39}, 
    'style': {'opacity': 0.38888888888888884}, 
    'type': 'Feature'} 

但是,這並不使用來自JSON的不透明度。我試圖執行該解決方案爲:

map.data.setStyle(function(feature) { 
    var value = feature.getProperty('opacity'); 
    var opacity = value; 
    return { 
     fillOpacity: opacity, 
     strokeWeight: opacity 
    }; 
}); 

哪些不起作用,錯誤Cannot read property 'data' of undefined

回答

1

既然你有你的polygonsgeoJSON格式我假設你使用google.maps.Data層來顯示它們。在這種情況下,您可以使用聲明式樣式規則根據其中一個屬性的值設置各個多邊形的樣式(有關更多信息,請參閱docs,查找「聲明式樣式規則」)。因此,舉例來說,你會:

map.data.setStyle(function(feature) { 
    var value = feature.getProperty('myProperty'); 
    var opacity = value <= 1 ? 0.1 : 1; 
    return { 
     fillOpacity: opacity, 
     strokeWeight: 1 
    }; 
}); 

如果你想有不透明1,如果你的myProperty的值越大則1,否則0.1。當然,你可以根據這個值計算任何不透明度,但是我從價值中計算不透明度只是一個例子。

如果這個答案不能滿足您的所有需求,請看看this SO answer,我會告訴您如何根據它們的id來更改單個邊界(多邊形)的樣式。

編輯 要回答你的問題的更新:你在做第一個錯誤是,你應該具有以下特徵的properties屬性中styles屬性,以便能夠通過feature.getProperty例如訪問它像這樣:

{'geometry': {'coordinates': [[[10.927456267537572, 45.68179119797432], 
    [10.940290010697588, 45.68157387892596], 
    [10.939979018768243, 45.67257819153854], 
    [10.927147329501077, 45.672795442796335], 
    [10.927456267537572, 45.68179119797432]]], 
'type': 'Polygon'}, 
'id': 1, 
'properties': { 
    'cellId': 39, 
    'style': {'opacity': 0.38888888888888884} 
}, 
'type': 'Feature'} 

那麼你的造型功能應該是這樣的:

map.data.setStyle(function(feature) { 
    var value = feature.getProperty('style'); 
    var opacity = value.opacity; 
    return { 
     fillOpacity: opacity 
    }; 
}); 
+0

確實爲一個單獨的多邊形該功能的工作,或爲所有這些? 'map.data.setStyle'不適用於整個地圖嗎? –

+1

是的,它適用於所有的多邊形,但顏色是基於屬性值,從你的問題,它聽起來像你想要的。也可以使用'map.data.overrideStyle()'函數爲各個多邊形着色,並允許您僅對所選特徵着色。但是您需要以某種方式獲得該功能的參考,這就是我在所引用的SO答案中所做的。 –

+0

我剛剛開始繼續爲此工作。在'map.data.setStyle(函數(特徵)'中,'feature' varible究竟是什麼?我想從geoJSON本身讀取不透明度值。 –