• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小码哥-倩倩
博客园    首页    新随笔    联系   管理    订阅  订阅

高德地图marker的点击事件

遇到问题:绘制高德地图,添加多个marker。并且给多个marker添加点击事件,点击的时候,无法检测到当前点击的marker。自己使用prototype添加属性并没有用。

解决办法:参考官方文档:https://lbs.amap.com/api/javascript-api/reference/overlay#marker

借助marker里的extData属性。用户自定义属性,支持JavaScript API任意数据类型,如Marker的id等

DEMO:

for(var j = 0,map;j < list.size; j++){
var myObj= list.data[j];
console.log(myObj);
var myLngLat=new AMap.LngLat(myObj.x,myObj.y);
console.log(myLngLat);
var marker = new AMap.Marker({
position: myLngLat,
map: map,
clickable: true,
extData:thit.allCommunityList[j]
});
marker.setLabel({
//修改label相对于maker的位置
offset: new AMap.Pixel(3, -25),
content: thit.allCommunityList[j].communityName,//markerContentCom
});
console.log(thit.allCommunityList[j]);
thit.currentSelectCommunity = thit.allCommunityList[j];
var clickHandle = AMap.event.addListener(marker, 'click', function(e) {
thit.cur =JSON.parse(JSON.stringify(e.target.getExtData()));
//得到的数据
});
}
posted @ 2019-02-25 19:42  小码哥-倩倩  阅读(29781)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3