官方文档
AMap.LabelsLayer图层参考手册
AMap.LabelMarker参考手册
标注和标注图层demo
标注和标注图层-文字标注demo
标注和标注图层-海量点demo
海量标注 LabelMarker
当需要在地图添加千级以上的点标记时,LabelMarker 是代替 Marker 的更好选择。不同于 MassMarks ,LabelMarker 不仅可以绘制图标,还可以为图标添加文字信息,且万级以上数据也具有较好性能,配置也更加灵活。
另外,LabelMarker 之间还支持避让功能,JSAPI 2.0 版本还支持地图标注避让 LabelMarker,可以让您的 marker 更加明显
创建 LabelsLayer
- LabelsLayer 图层是承载 LabelMarker 的图层,所有创建的 LabelMarker 需要添加到 LabelsLayer 图层上才能最终展示在地图上。
```javascript
// 创建设备图层方法
createLayer() {
this.layer = {};
// 创建 AMap.LabelsLayer 图层
this.layer = new AMap.LabelsLayer({
zooms: [3, 20],
zIndex: 111,
animation: true, // 淡入动画,默认为开启,v1.4.15 新增属性
collision: false, // 碰撞检测,默认为开启,v1.4.15 新getExtData增属性
});
// 将图层添加到地图
this.map.add(this.layer);
},
initMarkers(deviceType) {
let param = {
regionId: this.regionId,
regionLevel: this.regionLevel,
deviceType: deviceType
};
getDeviceList(param).then(res => {
let deviceList = res.data;
if (res.data == ‘’ || res.data.length == 0) {
return
}
//创建labelMarker海量点
this.multiMarkers = createLabelMarkers(map, param.deviceType, deviceList);
// 一次性将海量点添加到图层
layer.add(multiMarkers);
//海量点点击事件
for (let i = 0; i < multiMarkers.length; i++) {
} }); },this.multiMarkers[i].on('click', (e) => {
this.map.setCenter([e.lnglat.lng, e.lnglat.lat]);
})
// 切换设备时隐藏Markers hideMarkers() { for (let j = 0; j < this.multiMarkers.length; j++) { this.multiMarkers[j].hide(); this.layer.setMap(null) } },
```javascript
//循环数据创建LabelMarker
export function createLabelMarkers(map, deviceType, deviceList) {
let multiMarkers = [];
for (let i = 0; i < deviceList.length; i++) {
let curPosition = wgs84togcj02(deviceList[i].longitude, deviceList[i].latitude);
let icon = getIcon(deviceType, deviceList[i].liveStatus);
let labelMarker = new AMap.LabelMarker({
position: curPosition,
icon: icon,
extData: {data: deviceList[i], deviceType: deviceType},
name: deviceList[i]
});
multiMarkers.push(labelMarker);
}
return multiMarkers;
}
//获取地图上的设备Icon
function getIcon(deviceType, liveStatus) {
let img = getImg(deviceType, liveStatus);
let f36 = fontSize.f36
let f72 = fontSize.f72
let icon = {
type: 'image',
anchor: 'bottom-center',
image: img,
angel: 0,
retina: true,
size: [f36, f72]
};
return icon;
}
//获取地图上的设备Icon
function getImg(deviceType, liveStatus) {
let img;
switch (deviceType) {
case 2:
img = liveStatus == 1 ? 'rsu-point.png' : 'rsu-point_offline.png';
break
case 200:
img = liveStatus == 1 ? 'camera-point.png' : 'camera-point_offline.png';
break
case 202:
img = liveStatus == 1 ? 'radar-point.png' : 'radar-point_offline.png';
break
case 204:
img = liveStatus == 1 ? 'mec-point.png' : 'mec-point_offline.png';
break
case 203:
img = liveStatus == 1 ? 'lidar-point.png' : 'lidar-point_offline.png';
break
case 207:
img = liveStatus == 1 ? 'info-board-point.png' : 'info-board-point_offline.png';
break
case 208:
img = liveStatus == 1 ? 'waterSensor-point.png' : 'waterSensor-point_offline.png';
break
case 209:
img = liveStatus == 1 ? 'fogSensor-point.png' : 'fogSensor-point_offline.png';
break
default:
break
}
return '/platform/static/imgs/device/' + img;
}