How to add an event when googlemaps is dragged and calculate the distance from a previous point

Posted on February 22nd, 2010 by thiswayup.
Categories: dev.

Just to remind myself how to capture the event when the map is dragged AND work out the distance to a previously recorded center point on the map

GEvent.addListener(gmapFn.mapObj,'dragend',function(){
   var ctrLatLng = gmapFn.mapObj.getCenter();
   alert(ctrLatLng.distanceFrom(gmapFn.lastRefLatLng));
});

Very handy! gmapFn.mapObj = the object of the instianted gmap object and gmapFn.lastRefLatLng is a GLatLng. You can quickly get the center of a google map by calling the function gmapObject.getCenter() which returns the center as a GLatLng.

Enjoy!

Update : handy refrence of all the events you can to a google maps listener http://econym.org.uk/gmap/gevent.htm

0 comments.