效果图:
实现代码:
//加载img
map.loadImage(fangmu, function (error, image) {
if (error) throw error
map.addImage('fangmu', image, { sdf: true })
})
map.loadImage(wushuichuli, function (error, image) {
if (error) throw error
map.addImage('wushuichuli', image, { sdf: true })
})
map.loadImage(hedaolaji, function (error, image) {
if (error) throw error
map.addImage('hedaolaji', image, { sdf: true })
})
map.addSource('questionMapData', {
type: 'geojson',
data: questionMapData, //数据源
})
map.addLayer({
id: 'questionMapData', //图层id
type: 'symbol',
source: 'questionMapData',
layout: {
visibility: 'visible',
// 'text-field': ['get', 'name'],
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
'text-offset': [0, 1],
'text-anchor': 'top',
'icon-image': [ // icon图标,做判断,type_name为后端回传字段,判断type_name是否相等去显示图层
'case',
['==', ['get', 'type_name'], '污水处理设施'],
'wushuichuli', //图层名称
['==', ['get', 'type_name'],'河道垃圾'],
'hedaolaji',
['==', ['get', 'type_name'], '污水直排'],
'wushuizhipai',
['==', ['get', 'type_name'], '牲畜尸体倾倒'],
'shiti',
'buming', // default
],
'icon-size': 0.15,
},
// 修改文字颜色
paint: {
'icon-color': ['match', ['get', 'rectification_type'], 1, '#FFD700', 2, '#DC143C', 3, '#008000', '#D8BFD8'],
},
})