Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a Marker in Google Maps API v3

Tags:

I'm trying to remove a marker that was initialized like this:

marker = new google.maps.Marker({     position: latLng,     map: map,     draggable: true,     animation: google.maps.Animation.DROP,     title: 'Marker 1',     icon: redPin });  google.maps.event.addListener(marker, "click", function() {     showMarkerDialog(marker.position, "marker"); });  google.maps.event.addListener(marker, "dblclick", function() {     // Add a alert: Are you sure you want to remove this marker?      map.removeOverlay(marker); }); 

Everything works perfectly except that when I double click it to remove what I get on the Error Console is this:

TypeError: Object # has no method 'removeOverlay'

What am I doing wrong?

like image 329
Nathan Campos Avatar asked Nov 01 '11 01:11

Nathan Campos


People also ask

How do I remove a pointer from Google Maps?

When you use Google Maps to find a route to a destination, pins mark the starting point and the destination. Depending on the route, pins may also mark locations along the route, including detours. To remove any of these pins, right-click the Pin and select Remove this destination from the drop-down menu.

Can I turn off the markers in Google Maps?

Find the “Layers” menu in the bottom left corner of the screen. Hover your cursor over the box and wait until more options appear. Click “More” to open the Map Details menu. Under “Map Type,” you'll see a checked box next to “Labels.” Uncheck it to remove all labels.

How do you remove map marks?

If you want to remove the pin from Google Maps, simply right click on it and select "Remove this destination." Poof, it's gone.


1 Answers

There is no removeOverlay function on the map object. Sounds like you've got only one marker, why use an array? Just change this:

google.maps.event.addListener(marker, "dblclick", function() {     map.removeOverlay(marker); }); 

to this:

marker.addListener("dblclick", function() {     marker.setMap(null); }); 
like image 150
duncan Avatar answered Sep 20 '22 01:09

duncan