views:

65

answers:

4

I have the following code:

function clicked(overlay, latlng) {
  var icon3 = new GIcon();
  icon3.image = "marker.png";  
  icon3.iconAnchor = new GPoint(15, 40);

  var marker2 = new GMarker(latlng, {  icon: icon3, draggable: true,   title: 'Drag me'  });
   map.addOverlay(marker2);

}

Each time I click on the map a new marker is placed on the map. The problem is that I need only one marker and if I click several times, each time a new marker is added. How to change the code so only one marker is placed and when the map is clicked again it just changes its location?

A: 

You will need to implement some sort of check to see if the marker has already been placed. One option would be to keep a list of markers already added and check that list for a marker at a specific point before calling the functions to add the marker to the map. If you need a code sample, let me know.

Chris Thompson
I'm not looking for that, but just a simple (remove previous overlay function). There is not point in checking if the marker has been added - and it is rather impossible because each point have location specified by 2 numbers with 15 decimal places each and also you have to deal with database.
Vafello
+1  A: 

I would recommend keeping an instance of marker2 outside of the clicked function, then if marker2 is null, make and add a new one like you are now, otherwise call marker2.setLatLong(latlng); to update it's location.

Untested example code:

var marker2;

function clicked(overlay, latlng) {
  if (marker2 == null) {
    var icon3 = new GIcon();
    icon3.image = "marker.png";  
    icon3.iconAnchor = new GPoint(15, 40);

    marker2 = new GMarker(latlng, {  icon: icon3, draggable: true,   title: 'Drag me'  });
    map.addOverlay(marker2);
  }
  else {
    marker2.setLatLong(latlng);
  }
}
Andrew Koester
Good, but I think this one doesn't remove the overlay, i.e. the marker is still there.
Vafello
You didn't mention anything about overlays in your question.
Andrew Koester
O RLY? "How to change the code so only one marker is placed and when the map is clicked again it just changes its location"
Vafello
@Vafello, This answer will do that. The setLatLong method will move the marker. No need to remove it.
Chris Thompson
I think there is no such a method. Maybe setLatLng, however both don't work as required:/I just get multiple markers.
Vafello
Maybe you're using an older version of GMaps (setLatLong was added in 2.88, apparently) Try setPoint(latlng); instead of setLatLong();.
Andrew Koester
For some strange reason I cannot execute else statement, i.e. my marker is always null and it is placed on the map each time. I tried to execute alert('test); in my else atatement ut it never works.
Vafello
Unless I'm missing something, this is exactly the solution you seem to be asking for: If the marker exists, move it to the new location, if not, create it at the new location. You should try using this solution.
Daniel Beardsley
A: 

I think that it should test if marker2 is null and if not, removeOverlay(marker2) and add new one.

Vafello
A: 

Well, dont bother, I figured it out myself. I just used map.clearOverlays(); before the marker is pplaced and that solved the problem.

Vafello