Chris is right, you should have only one marker and change it's
position
but to answer your question about your existing code, something like
that should work better :
var prevMarker = null;
var currentMarker = null;
google.maps.event.addListener(map, "click", function(event) {
prevMarker = currentMarker;
currentMarker = placeMarker(event.latLng);
});
function placeMarker(location) {
if (prevMarker != null) {
prevMarker.setMap(null)
}
var marker = new google.maps.Marker({
position: location,
map: map
});
return marker;
};
};
On Jul 20, 7:58 am, Chris Broadfoot <[email protected]> wrote:
> Why not just change the position of the existing marker?
>
> --http://twitter.com/broady
>
>
>
>
>
>
>
> On Wed, Jul 20, 2011 at 3:31 PM, jtomasrl <[email protected]> wrote:
> > So when I make click on the map a new marker is added, but if there's
> > a prev marker, it get deleted
>
> > var prevMarker = null;
> > google.maps.event.addListener(map, "click", function(event) {
> > placeMarker(event.latLng);
> > prevMarker = event.serviceObject;
> > });
>
> > function placeMarker(location) {
> > var clickedLocation = new google.maps.LatLng(location);
> > if (prevMarker != null) {
> > prevMarker.setMap(null)
> > }
> > var marker = new google.maps.Marker({
> > position: location,
> > map: map
> > });
> > };
> > };
>
> > But the code is just adding markers but no deleting previous ones
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Maps JavaScript API v3" group.
> > To post to this group, send email to
> > [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/google-maps-js-api-v3?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.