遇到问题:绘制高德地图,添加多个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()));
//得到的数据
});
}