mapbox加载geojson点位,点击点,该点高亮(点位图片放大)

let sourceData : AnyObject = {
	type: 'FeatureCollection',
	features: [],
};
/**
 * @Description: 撒点方法,地图加载geojson数据
 * @Author: wms
 * @param {any} mapObj 地图对象
 * @param {string} layerId 图层id
 * @param {any} sourceData 资源data
 * @param {string | string[]} icon 图标图片
 * @param {number | string[]} size 图标大小
 * @param {string} field 图标名称
 * @param {string} field1 鼠标移入图标名称
 * @param {string} textColor 图标名称
 * @param {boolean} textOverlap 文字避让
 * @param {any} textSize 文字大小
 * @param {any} textOffset 文字偏移量
 * @param {string} isVisibile 是否显示
 */
function addPointLayer(
	mapObj: any,
	layerId: string,
	sourceData: any,
	icon: string | string[],
	size: number | string[],
	field: string,
	field1: string,
	textColor: string,
	textOverlap: boolean,
	textSize: any,
	textOffset: any,
	isVisibile: string,
) {
	mapObj.addLayer({
		id: layerId,
		type: 'symbol',
		source: {
			type: 'geojson',
			data: sourceData,
		},
		layout: {
			'icon-image': icon, // 图片资源地址
			'icon-size': size, // 图片大小
			'icon-offset': [0, -60], // 图片偏移量
			'icon-allow-overlap': true, // 图片是否避让
			'text-allow-overlap': textOverlap, // 文字是否避让
			'text-optional': true, 
			'text-field': field, 
			'text-font': ['Microsoft YaHei Bold'],
			'text-size': textSize,
			'text-offset': textOffset,
			visibility: isVisibile,
		},
		paint: {
			'text-color': textColor,
			'text-halo-color': '#000',
			'text-halo-width': 1,
		},
	});
    
    // 鼠标移入显示field文字,移出则显示field1文字
	mapObj.on('mouseenter', layerId, () => {
		mapObj.setLayoutProperty(layerId, 'text-field', field);
	});
	mapObj.on('mouseleave', layerId, () => {
		mapObj.setLayoutProperty(layerId, 'text-field', field1);
	});
}
/**
 * @Descripttion: 点击点位图片添加放大图片图层
 * @param {any} map
 * @param {any} features 选中数据的features
 */
function addPointHighlight(map: any, features?: any[]) {
	features[0].layer.layout['icon-size'] =
		features[0].layer.layout['icon-size'] + 0.2;
	if (features[0].layer.layout['icon-image'].name) {
		features[0].layer.layout['icon-image'] = ['get', 'url'];
	}
	features[0].layer.layout['visibility'] = 'visible';

	map.setLayoutProperty(features[0].layer.id, 'icon-allow-overlap', false);

	if (!map.getSource('pointHighlight')) {
		map.addLayer({
			id: 'pointHighlight',
			type: 'symbol',
			source: {
				type: 'geojson',
				data: {
					type: 'FeatureCollection',
					features: [],
				},
			},
			layout: features[0].layer.layout,
			paint: {
				'text-color': '#fff',
				'text-halo-color': '#000',
				'text-halo-width': 1,
			},
		});
	}


/**
 * @Descripttion: 移除放大图片图层
 * @param {any} map
 * @param {any[]} features 选中数据的features
 */
function removePointHighlightLayer(
	map: any,
	features: any[],
) {
    // 判断是否有该图层,有该图层则修改图层属性,再移除该图层和图层资源
	if (map.getSource('pointHighlight')) {
		if (features.length != 0)
			map.setLayoutProperty(
				features[0].layer.id,
				'icon-allow-overlap',
				true,
			);
		map.removeLayer('pointHighlight');
		map.removeSource('pointHighlight');
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小满blue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值