detect marker and move if exists

1,753 views
Skip to first unread message

Kile Design

unread,
Mar 7, 2011, 10:45:36 PM3/7/11
to google-map...@googlegroups.com
Hi all.
Is there a easy way to detect if a map has a marker or not? I need to allow only one marker and just update it's position if the address is changed (from a call from a form text field).

Got most part up'n'running, but now it just adds more markers when I change the address... need to prevent that...
Hope someone has a good practice on this... thanks...

Esa

unread,
Mar 7, 2011, 11:29:56 PM3/7/11
to Google Maps JavaScript API v3
Create just a single marker and recycle that by setting its position.

Kile Design

unread,
Mar 7, 2011, 11:53:12 PM3/7/11
to google-map...@googlegroups.com
Now how do I create just a singe marker? heh... I'm doing it this way at the moment:

var aBusinessMarker = new google.maps.Marker({ draggable: true, map: map, position: results[0].geometry.location});

As default there's no marker on the map. But when a user types into the "address"-field, and leaves the field (onBlur-trigger), the map updates and places the marker. Now, if the user goes back to the field and types another address; Just change the existing marker location... hehe...

I've just worked with google maps for a couple of days here, so if it is in the documentation somewhere - well explained - so please tell/show me where...

Esa

unread,
Mar 8, 2011, 9:11:20 AM3/8/11
to Google Maps JavaScript API v3
If you change that line to following, the marker will be created only
at the first onBlur. Second+ time it doesn't do anything.

var aBusinessMarker = aBusinessMarker || new
google.maps.Marker({ draggable: true, map: map,
position: results[0].geometry.location});

Add a second line that positions the marker every time:

aBusinessMarker.setPosition(results[0].geometry.location);

If you post a link to your page under work, you get better advice.
Message has been deleted

Kile Design

unread,
Mar 9, 2011, 3:54:51 AM3/9/11
to google-map...@googlegroups.com
Hi again...
Finally got the time to put up a page here : http://thomaskile.me/testarea/google-maps-registration/

So I tried what you suggested, but it keeps adding more markers... Tried with some if/else statements, but got same result at best. If not, the whole thing stopped working for me...
The google javascript file is named: tmp.googleMaps.js

Rossko

unread,
Mar 9, 2011, 6:21:06 AM3/9/11
to Google Maps JavaScript API v3
> So I tried what you suggested, but it keeps adding more markers...

If you create your lone marker as a variable in global scope, you will
be able to access it to re-use it later.

Kile Design

unread,
Mar 9, 2011, 9:48:56 AM3/9/11
to google-map...@googlegroups.com
wow! so easy...
thanks a lot Rossko... now everything seems to be working the way I want... awesome...
Reply all
Reply to author
Forward
0 new messages