高德地图marker标记点的setLabel,在不同层级下控制显示隐藏

vue中使用高德地图marker标记点的setLabel,在不同层级下控制显示隐藏
设置点标注的文本标签
marker.setLabel({
  offset: new AMap.Pixel(1, 0), //设置文本标注偏移量
  content: '<div>' + name + '</div>', //设置文本标注内容
  direction: 'top', //设置文本标注方位
});
1
2
3
4
5
监听地图层级变化
let _this = this;
var logMapChange = function () {
  var mapZoom = _this.map.getZoom(); //获取当前地图级别
  if (mapZoom < 15) {
    _this.hideMarkerLabel(marker);
  } else {
    _this.showMarkerLabel(marker);
  }
};
_this.map.on('zoomchange', logMapChange); //监听
1
2
3
4
5
6
7
8
9
10
标签显示


showMarkerLabel(marker) {
    marker.setLabel({
        offset: new AMap.Pixel(1, 0), //设置文本标注偏移量
        content: '<div>' + marker._originOpts.title + '</div>', //设置文本标注内容
        direction: 'top', //设置文本标注方位
    });
}
1
2
3
4
5
6
7
标签隐藏


hideMarkerLabel(marker) {
  marker.setLabel({
    offset: new AMap.Pixel(1, 0), //设置文本标注偏移量
    content: '',
    direction: 'top', //设置文本标注方位
  });
}
1
2
3
4
5
6
7
标签样式外部写入
.amap-marker-label {
  padding: 2px 6px;
  border-radius: 5px;
  box-shadow: 0 2px 6px 0 rgba(114, 124, 245, 0.5);
  text-align: center;
  font-size: 14px;
  border: 0;
}
1
2
3
4
5
6
7
8
第二种方法
也可以在content: '< div />'里写入style,动态传参绑定display属性的’none’和’null’值
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以在高德地图Marker对象中添加鼠标事件监听器,来实现鼠标移入显示label详细信息的功能。具体的实现步骤如下: 1. 创建Marker对象时,设置label属性为一个AMap.Text对象,用于显示详细信息。 ```javascript var marker = new AMap.Marker({ position: [lng, lat], label: { content: '详细信息', offset: new AMap.Pixel(0, -20) // 设置label偏移量,使其显示Marker上方 } }); ``` 2. 给Marker对象添加mouseover和mouseout事件监听器,在鼠标移入和移出时显示隐藏label。 ```javascript marker.on('mouseover', function (e) { marker.setLabel({ content: '详细信息', offset: new AMap.Pixel(0, -20), visible: true // 显示label }); }); marker.on('mouseout', function (e) { marker.setLabel({ visible: false // 隐藏label }); }); ``` 完整的代码示例: ```javascript var map = new AMap.Map('container', { zoom: 13, center: [116.397428, 39.90923] }); var markers = [ {lnglat: [116.405285, 39.904989], info: '北京市'}, {lnglat: [121.472644, 31.231706], info: '上海市'}, {lnglat: [113.280637, 23.125178], info: '广州市'}, {lnglat: [114.066112, 22.548515], info: '深圳市'} ]; for (var i = 0; i < markers.length; i++) { var marker = new AMap.Marker({ position: markers[i].lnglat, label: { content: markers[i].info, offset: new AMap.Pixel(0, -20) } }); marker.on('mouseover', function (e) { marker.setLabel({ content: e.target.getExtData().info, offset: new AMap.Pixel(0, -20), visible: true }); }); marker.on('mouseout', function (e) { marker.setLabel({ visible: false }); }); marker.setExtData(markers[i]); // 将详细信息存储在Marker对象的扩展数据中 marker.setMap(map); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值