添加高亮之前,先移除高亮效果;
必须允许图标避让,否则会出现与原图标重叠问题。
export function addPointHighlight(map: AnyObject, id: string, geo: AnyObject) {
setTimeout(() => {
const layer = map.getLayer(id);
if (layer) {
const lay = JSON.parse(JSON.stringify(layer.layout._values));
const pai = JSON.parse(JSON.stringify(layer.paint._values));
const layout = {},
paint = {};
Object.keys(lay).map((key) => {
if (map.getLayoutProperty(id, key)) {
layout[key] = map.getLayoutProperty(id, key);
}
});
Object.keys(pai).map((key) => {
if (map.getPaintProperty(id, key)) {
paint[key] = map.getPaintProperty(id, key);
}
});
map.setLayoutProperty(id, 'icon-allow-overlap', false);
layout['icon-allow-overlap'] = true;
layout['icon-size'] = layout['icon-size'] + 0.2;
layout['visibility'] = 'visible';
if (!map.getSource('pointHighlight')) {
map.addLayer({
id: 'pointHighlight',
type: 'symbol',
source: {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [],
},
},
layout: layout,
paint: paint,
});
}
map.setLayoutProperty(
'pointHighlight',
'icon-image',
layout['icon-image'],
);
map.setLayoutProperty(
'pointHighlight',
'icon-size',
layout['icon-size'],
);
map.getSource('pointHighlight').setData(geo);
}
}, 0);
}
export function removePointHighlightLayer(map: AnyObject, id: string) {
if (map.getSource('pointHighlight')) {
map.setLayoutProperty(id, 'icon-allow-overlap', true);
map.removeLayer('pointHighlight');
map.removeSource('pointHighlight');
}
}