百度地图Api中
除覆盖物有两个方法:map.removeOverlay()或者 map.clearOverlays(),其中
clearOverlays()方法一次移除所有的覆盖物
removeOverlay()一次移除一个指定覆盖物
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-size:14px;} #allmap {width:100%;height:500px;} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=AE01cf80f7e34194eb200a42443ed923"></script> <title>逆地址解析</title> </head> <body> <div id="allmap"></div> <p >点击地图展示详细地址:<span id="text" style="color: red;"></span></p> <button onclick="oneDell(1)">删除第一个marker</button> <button onclick="oneDell(3)">删除第三个marker</button> </body> </html> <script type="text/javascript"> // 百度地图API功能 var text = document .getElementById ("text"); var markerArr = []; var map = new BMap.Map("allmap"); var point = new BMap.Point(116.331398,39.897445); map.centerAndZoom(point,12); map.enableScrollWheelZoom(true); var geoc = new BMap.Geocoder(); var index = 0 map.addEventListener("click", function(e){ var pt = e.point; geoc.getLocation(pt, function(rs){ var addComp = rs.addressComponents; text.innerHTML = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber; alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); }); var marker = new BMap.Marker(pt); // 创建标注 index = ++index marker['id'] = index; markerArr.push(marker); map.addOverlay(marker); }); function oneDell(data){ for (var i = 0; i < this.markerArr.length; i++) { console.log(markerArr[i].id) if (markerArr[i].id== data) { map.removeOverlay(markerArr[i]) } } } </script>
最近项目一期上线,同时自己搬了新家 新的开始,安逸了,又不想奋斗了,哎 好好学习吧