方法是,给geojson中的每一个feature的propreties设置一个color属性,用于承载不同的颜色,然后在layer加载时候,给paint使用["get","color"],具体代码如下:
var myLocation={
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id":1,
"name": "kunming",
"color":"red"
},
"geometry": {
"coordinates": [102.73,25.04],
"type": "Point"
}
},
{
"type": "Feature",
"properties": {
"id":10,
"name": "kunming",
"color":"green"
},
"geometry": {
"coordinates": [102.83,25.04], //获取color字段,设置点的颜色
"type": "Point"
}
}
]
};
map.addSource('mySource', { type: 'geojson', data: myLocation });
map.addLayer({
"id": "myLayer",
"type": "circle",
"source": "mySource",
"paint": {
"circle-color": ["get",'color']
}
});