Search code examples
javascriptgoogle-mapsgoogle-maps-api-3jquery-ui-map

Delete/move Google maps marker (jquery-ui-maps)


I want to remove one marker from my Google Map, but I can't seem to get it to work. I find various answers, all telling me to use .setMap(null) on the marker, but I can't seem to get it to work.

$map_canvas = $('#map_canvas');
var youreHere_Marker;
function centerMapToAddress( address ) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if( typeof youreHere_Marker!=="undefined"){
                youreHere_Marker.setMap(null);
            }
            youreHere_Marker = $map_canvas.gmap('addMarker', {'position': results[0].geometry.location.lat()+','+results[0].geometry.location.lng(), 'bounds': true});
        }
    });
}

I get TypeError: youreHere_Marker.setMap is not a function. To my knowledge this means that the variable youreHere_Marker doesn't have the method .setMap(), but if I do console.log(youreHere_Marker) and inspect the object, I can see the method.

I have more markers on my map, via the MarkerClusterer. Those should remain untouched

I have the feeling I'm close, could someone point me in the right direction?

Edit: I've also tried .setPosition(), same error. I'm assuming I'm using the variable incorrect, but I don't know how to refer to it properly.


Solution

  • Well, i was working with google maps without jQuery, but i think (i'm not sure, but you may try) that you should get your marker with the following code:

    youreHere_Marker = $map_canvas.gmap('get', 'markers')[0];
    youreHere_Marker.setMap(null);
    

    I'm really not sure that it will do what you want, but there is a possibility that this will work : )
    I hope you'll solve you problems.
    Thanks. : )