Reputation: 903
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 ..
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 :)
Upvotes: 0
Views: 217
Reputation: 221
I think deleteCurrentMarkers()
will never be fired 2nd time cose you fire it only when setRelayMap()
is fired. You should fire in "dragend" callback or in getDataFromServer()
Upvotes: 1
Reputation: 1874
When you call deleteCurrentMarkers method your markerHome variable is null. So the code doesn goes into if statement.
function deleteCurrentMarkers(){
if (markerHome != null){ //markerHome is null here!!!
console.log("set markerHome to null");
markerHome.setMap(null);
markerHome = null;
}
}
Upvotes: 1