0

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.

Martijn
  • 15,791
  • 4
  • 36
  • 68

1 Answers1

0

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. : )

  • Not really sure why, but this did the job. Am I right if I assume that `.gmap('get', 'markers')[0]` returns the last added marker? – Martijn Apr 04 '16 at 09:52
  • @Martijn I was unable to find docs for jquery-ui-map, but i found some sources and thought that gmap('addMarker', .... doesn't return the marker that was already created, and to get it we should use another method (like gmap('get', 'markers'), but the last method returns all markers, that's why we just taking the first one : ) – Eugene Korobov Apr 04 '16 at 10:08
  • Marked answer as solution. If you do find any documentation, or can explain it well enough yourself, you'll get more upvotes :) – Martijn Apr 04 '16 at 10:16