Tuesday, April 10, 2007

GMap Animation and Events -II: Events

A quick look at events supported by GMap. GMap supports event handling through the Event listener pattern. The class that provides support for event handling is GEvent class. The addEventListener() method of GEvent class. It takes three parameters:

1. Object of GMap:

The map on which event has to be applied.

2. The event to be Handled:

It is a string. The string contains event names. There are two commonly used events:

i. click:

It is the most common of all the events. It is generated (or happens) when one presses and releases the left mouse button on the map.

ii. moveend:

It is generated when the user stops navigating or panning the map. It essentially means end of moving a map.

Following code shows how to use both types of events:

The code is a slight modification of the initial code to display map.

function load() {

if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById("map"));

//map.setMapType(GMapType.G_HYBRID_MAP);

map.addControl(new GSmallMapControl());

map.addControl(new GMapTypeControl());

map.addControl(new GScaleControl());

map.setCenter(new GLatLng(31.122027, 77.111664), 13);

GEvent.addListener(map,"click", function(){

alert(map.getCenter())

}

);

//for the event moveend

GEvent.addListener(map,"moveend", function(){

alert(map.getCenter())

}

)

}

}

The getCenter() method of the GMap2 returns the current focus latitude and longitude of the map. That’s all for this quickie.

No comments: