問題描述
對于熟悉 Leaflet 的人,您知道動態更改多邊形顏色的方法嗎?例如,以這樣定義的圓為例:
For anyone who is familiar with Leaflet, do you know a way to dynamically change a polygon's color? For example, take a circle defined like this:
window.circle = L.circle([51.508, -0.11], 500, {
color: 'red',
fillColor: '#ffffff',
fillOpacity: 0.5
}).addTo(map);
然后,在用戶單擊界面上某處的按鈕后(例如),我想像這樣更改圓圈的顏色:
Then later, after a user clicks a button somewhere on an interface (for example), I want to change the color of the circle like this:
window.circle.options.fillColor = "#dddddd";
代碼更改了 window.circle.options.fillColor 的值,但更改不會反映在地圖上多邊形顏色的更改中.我四處尋找,但沒有找到任何東西.有什么想法嗎?
The code changes the value for window.circle.options.fillColor, but the change is not reflected by a change to the color of the polygon on the map. I've searched around but haven't found anything. Any ideas?
謝謝.
推薦答案
L.Circle
extends L.Path
(http://leafletjs.com/reference.html#path),有方法 setStyle( <Path options> object )
,你可以將新樣式應用為 window.circle.setStyle({fillColor: '#dddddd'});
L.Circle
extends L.Path
(http://leafletjs.com/reference.html#path), that have method setStyle( <Path options> object )
, and you can apply new style as window.circle.setStyle({fillColor: '#dddddd'});
這篇關于動態更改傳單中多邊形的顏色?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!