gpt4 book ai didi

javascript - 删除百度 map js Api 中的标记

转载 作者:行者123 更新时间:2023-11-29 23:26:24 25 4
gpt4 key购买 nike

我想删除我添加到 map 上的标记。但是,当我通过 javascript 代码中调用的函数添加标记时,我可以正确删除它,但是当我通过添加按钮添加标记时,我无法删除该标记。这两行代码有什么区别吗?百度 map 给出的删除demo:Delete marker

下面是我的代码:

<!DOCTYPE html>
<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:"微软雅黑";}
#allmap{width:100%;height:500px;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=38XkbdMoTDz7oYXNxnmcyvXLFNgIuDNv"></script>
<title>Delete specific marker</title>
</head>
<body>
<div id="allmap"></div>

<input type="button" onclick="addMarker(116.417029, 39.903252,null,'123');" value="add"/>
<input type="button" onclick="deletePoint('123')" value="delete id=123"/>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
map = new BMap.Map("allmap");
point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point, 15);
map.disableDoubleClickZoom(true);

// 编写自定义函数,创建标注
function addMarker(lng, lat,iconName, labelContent){
var p = new BMap.Point(lng, lat);
var marker = new BMap.Marker(p, {icon:iconName});
var label = new BMap.Label(labelContent,{offset:new BMap.Size(20,-10)});
map.addOverlay(marker);
marker.setLabel(label);
//marker.setTitle(labelContent);
marker.setAnimation(BMAP_ANIMATION_BOUNCE);
}
var commandPeopleIcon = new BMap.Icon("icon/people.png", new BMap.Size(47,37));
var tankIcon = new BMap.Icon("icon/tank.png", new BMap.Size(50, 50));
// 随机向地图添加25个标注
var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var lngSpan = Math.abs(sw.lng - ne.lng);
var latSpan = Math.abs(ne.lat - sw.lat);
for (var i = 0; i < 0; i++) {
var lng = sw.lng + lngSpan * (Math.random() * 0.7);
var lat = ne.lat - latSpan * (Math.random() * 0.7);

addMarker(lng, lat, tankIcon, "id="+i);
}
function deletePoint(agentName){
var allOverlay = map.getOverlays();
alert(allOverlay.length);
for (var i = 0; i < allOverlay.length -1; i++){
//var name = allOverlay[i].getLabel().content;
//var title = allOverlay[i].getTitle();
//alert(name);
var className = allOverlay[i]["V"]["classList"][0];
//alert(className);
if (className == 'BMap_Marker') {
var label = allOverlay[i].getLabel();
if (label == null) {
alert('label is null');
continue;
}
alert(allOverlay[i].getLabel().content);
if(allOverlay[i].getLabel().content == agentName){
alert('delete');
map.removeOverlay(allOverlay[i]);
return false;
}
}
}
}
// if I add marker here, I can click delete button to delete the marker.
//addMarker(116.417029, 39.903252,null,'123');
</script>

最佳答案

要添加标记,

var mp = new BMap.Map("container");
var marker = new BMap.Marker(point);
mp.addOverlay(marker);

要删除标记,

mp.removeOverlay(marker);

关于javascript - 删除百度 map js Api 中的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49097477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com