[英]Trouble defining fill color for geojson attribute in Mapbox / Leaflet
[英]defining a polygon fill color based on geojson with mapbox
我正在使用mapbox示例以在地圖上創建多個多邊形,並且每個都有彈出事件。 我的問題是我需要根據它的geojson屬性設置每個多邊形的填充顏色。
這是我的榜樣。 我使用以下javascript代碼:
mapboxgl.accessToken = 'pk.eyJ1IjoibWFoYW5tZWhydmFyeiIsImEiOiJ6SDdSWldRIn0.8zUNm01094D1aoSeHpWYqA';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [51.40545845031738,
35.75069181054449],
zoom: 10
});
map.on('load', function (e) {
// Add a layer showing the state polygons.
map.addLayer({
'id': 'states-layer',
'type': 'fill',
'source': {
'type': 'geojson',
'data': 'geojson.js'
},
'paint': {
'fill-color': 'rgba(200, 100, 240, 0.4)',
'fill-outline-color': 'rgba(200, 100, 240, 1)'
}
});
// When a click event occurs on a feature in the states layer, open a popup at the
// location of the click, with description HTML from its properties.
map.on('click', 'states-layer', function (e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
//.setHTML(e.features[0].properties.name)
.setHTML("<h1>"+e.features[0].properties.userone+"</h1>"+e.features[0].properties.name)
.addTo(map);
});
// Change the cursor to a pointer when the mouse is over the states layer.
map.on('mouseenter', 'states-layer', function () {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', 'states-layer', function () {
map.getCanvas().style.cursor = '';
});
});
在這里它加載所有相同的顏色
'paint': {
'fill-color': 'rgba(200, 100, 240, 0.4)',
'fill-outline-color': 'rgba(200, 100, 240, 1)'
}
在我的geojson文件中,我有一個顏色鍵:
"type": "Feature",
"properties": {
"userone":"پیروزی",
"name":"North Dafkota",
"featureclass":"Admin-1 scale rank",
"color":"red"
}
我想用它來定義多邊形填充顏色。
如果您只想使用在geojson要素屬性中定義的顏色。 然后你可以像這樣使用層標識屬性:
map.addLayer({ 'id': 'states-layer', 'type': 'fill', 'source': { 'type': 'geojson', 'data': 'geojson.js' }, 'paint': { 'fill-color': { type: 'identity', property: 'color', }, 'fill-outline-color': 'rgba(200, 100, 240, 1)' } });
另請參閱: https : //www.mapbox.com/mapbox-gl-js/style-spec/#function-type
並且: https : //www.mapbox.com/mapbox-gl-js/style-spec/#types-color
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.