javascript高德地图实现点击marker消失marker
<pre>
var markers = [];
var positions = [[120.17718, 30.21772], [120.17718, 30.21872]];
for (var i = 0, marker; i < positions.length; i++) {
marker = new AMap.Marker({
map: map,
icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
position: positions[i],
content: '<div class="marker-route marker-marker-bus-from zhongzimark" zhongziid=1><img class="markerimg" src="http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png" /><div class="markarea"></div></div>' //自定义点标记覆盖物内容
});
markers.push(marker);
markers.push(marker);
}
$.each(markers, function (k, v) {
markers[k].on('click', function () {
//获取html属性方法
var htmlc=markers[k].getContent();
htmlc=$(htmlc);
console.log($(htmlc).attr('zhongziid'))
if ( markers[k]) {
markers[k].setMap(null);
markers[k] = null;
}
/* var str2 = {"personid": <?php echo $personid ?>, "type": 1, "shuzhi": 10, "tag": 3};
str2 = JSON.stringify(str2);
ws.send(str2);*/
});
})
<style>
.zhongzimark {
position: relative;
width: 60px;
height: auto;
overflow: hidden;
}
.markerimg{
width:15%;
}
.markarea{
width:15%;
height:auto;
overflow:hidden;
line-height: 1.3;
text-align: center;
color: #000;
background: #FFF;
}
</style>
</pre>
ps:原理就是 先循环遍历marker 显示并且添加到markers数组 然后循环遍历markers数组添加事件就好了 getContent可以获取到html 然后用jquery attr方法获取属性
作者:newmiracle