gpt4 book ai didi

javascript - 删除标记谷歌地图不起作用 API v3

转载 作者:行者123 更新时间:2023-12-02 17:58:18 25 4
gpt4 key购买 nike

我目前正在研究谷歌地图,我遇到了一个我自己无法解决的问题。我一直在关注 stackOverflow 上的一些文档和答案,但我无法让它正常工作..

我的 map 上只有一个标记,我想要做的就是在每次拖动事件触发时将其删除并放置一个新的标记...

当我调试代码时,似乎我的标记没有从 map 上删除,或者我没有..我真的不明白这种行为..

所以这是代码:

我的谷歌地图实例:

    var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: levelZoom,
center: new google.maps.LatLng(50.6548670, 3,1888100),
});

操作:

var markerHome = null;

function deleteCurrentMarkers(){
if (markerHome != null){
console.log("set markerHome to null");
markerHome.setMap(null);
markerHome = null;
}
}

function setRelayMap(dataSource, currentCoords, myFilterObject){
var i;
var lt = currentCoords.lat;
var lg = currentCoords.lng;


deleteCurrentMarkers();
markerHome = new google.maps.Marker({position: new google.maps.LatLng(lt, lg), map: map, icon: "icoMaps/initPos.png"});
google.maps.event.addListener(map, 'dragend', function() {
var centerObj = map.getCenter();
var newObj = {"lat": centerObj.lat(), "lng": centerObj.lng()};
console.log("in dragend");
getDataFromServer(newObj, myFilterObject);
});
google.maps.event.addListener(map, 'zoom_changed', function() {
levelZoom = map.getZoom();
});
}

你能看到什么可能导致我的问题吗?

谢谢你的帮助:)

最佳答案

当您调用deleteCurrentMarkers方法时,您的markerHome变量为空。因此代码不会进入 if 语句。

function deleteCurrentMarkers(){
if (markerHome != null){ //markerHome is null here!!!
console.log("set markerHome to null");
markerHome.setMap(null);
markerHome = null;
}
}

关于javascript - 删除标记谷歌地图不起作用 API v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20802998/

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