简体   繁体   English

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

[英]removing marker google map doesn't work API v3

i'm currently working on google map and i'm going through a problem i can't solve myself. 我正在研究谷歌地图,我正在经历一个我无法解决的问题。 i've been following some docs and answer on stackOverflow but i can't get it work properly .. 我一直在关注stackOverflow上的一些文档和答案,但我无法正常工作..

i have one and only one marker on my map, all i want to do is removing it to put a new one each time the dragent event is firing ... 我在我的地图上只有一个标记,我想要做的就是删除它以在每次触发戏剧事件时添加一个新标记...

When i'm debugging the code, it seems like my marker ain't deleted from the map or i don't .. i don't really understand the behavior .. 当我调试代码时,似乎我的标记没有从地图中删除,或者我没有...我真的不明白行为..

So here is the code : 所以这是代码:

my google map instance : 我的谷歌地图实例:

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

the manip : 操纵:

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();
});
    }

can you see anything that could cause my problem ? 你能看到任何可能导致我问题的事吗?

thank you for helping :) 感谢您的帮助 :)

When you call deleteCurrentMarkers method your markerHome variable is null. 当您调用deleteCurrentMarkers方法时,您的markerHome变量为null。 So the code doesn goes into if statement. 因此代码不会进入if语句。

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

I think deleteCurrentMarkers() will never be fired 2nd time cose you fire it only when setRelayMap() is fired. 我认为deleteCurrentMarkers()将永远不会被解雇第二次COSE你火了,只有当setRelayMap()被触发。 You should fire in "dragend" callback or in getDataFromServer() 你应该在“dragend”回调或getDataFromServer()触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM