move marker on zoom

56 views
Skip to first unread message

Damian

unread,
Aug 31, 2011, 10:21:35 PM8/31/11
to google-map...@googlegroups.com
Hi all, I have a draggable marker so the user can dynamically select lat / long values. However if the user zooms into a location, the marker usually ends up off to the side somewhere out of view, and is difficult to find. Is there anyway to automatically move the marker so that it always stays within view when the user zooms in to a location? Cheers, Damian

xelawho

unread,
Sep 1, 2011, 4:21:42 PM9/1/11
to Google Maps JavaScript API v3
> Is there anyway to automatically move the marker so that it always stays within
> view when the user zooms in to a location?

I see there is a bounds_changed event listed for the map object

I assume that one possible way would be to add a listener for when the
bounds have changed that sets the marker's position to the new map
center.

(which would do the same for if the map was dragged or moved in any
other way - possibly the zoom_changed event would be more useful...)

Damian

unread,
Sep 27, 2011, 12:17:21 AM9/27/11
to google-map...@googlegroups.com
Hi xelawho, thanks for responding andsorry for taking so long (been away). I'm a total newbie when it comes to javascript, could you (or anyone else for that matter) possibly suggest some code for me? Cheers, D

xelawho

unread,
Sep 27, 2011, 1:02:28 AM9/27/11
to Google Maps JavaScript API v3
yeah, actually I was playing around with this while you were gone. I
found that it was too annoying to move the marker every time somebody
did something to the map, and it only really made sense to only move
it if the marker disappeared outside of the map bounds. Have a look:
http://www.xelawho.com/map/markermove.htm

Damian

unread,
Sep 27, 2011, 1:24:46 AM9/27/11
to google-map...@googlegroups.com
Ah, brilliant, exactly what I was after. Thanks for that. Perhaps you should post the code just in case your link ever goes down (mainly for the benifit of other newbs like me).
Reply all
Reply to author
Forward
0 new messages