gpt4 book ai didi

javascript - 删除特定标记 APIv3

转载 作者:行者123 更新时间:2023-11-28 16:02:43 25 4
gpt4 key购买 nike

如何删除谷歌地图中的特定标记?我尝试了下面的代码,但收到 TypeError:marker[1] is undefined 错误。如果没有 [1],它只会删除第一个标记。

google.maps.event.addDomListener(deletemark, 'click', function() {
marker[1].setMap(null);
});
}

(来自评论,稍作编辑)这就是我创建标记以及标记的经纬度数组的方式

var locations = [ [56.066877,21.069274], [55.279849,26.048155] ]; // removed "hanging comma"
for (i = 0; i < locations.length; i++)
{
marker = new MarkerWithLabel({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map // removed "hanging comma"
});
} // added missing close bracket

最佳答案

尝试使用数组来存储标记,

var markerArray=[];
var locations = [ [56.066877,21.069274], [55.279849,26.048155] ];
for (i = 0; i < locations.length; i++)
{
marker = new MarkerWithLabel({
position: new google.maps.LatLng(locations[i][0], locations[i][1]),
map: map // removed "hanging comma"
});
markerArray.push(marker);
} /

以及删除标记,

function deleteMarkers() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}

关于javascript - 删除特定标记 APIv3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366657/

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