Server events
With server events, you will be able to travel from the map to the server by means of AJAX. The use of the server events is the same as any ASP.NET Web control.
by default server events are deactivated.
It is necessary to explicitly activate them with enableServerEvents = true
As you can see in the example, the functions that respond to the event give back code in Javascript. We also see that there are two parameters in the function:
- s: it is the source that has produced the event. Usually it is the identifier of the map, so that we can use it to execute Javascript.
- e: arguments. The common denominator of all the arguments is:
- who: it coincides with the source previously described. It indicates who has sent the event.
- point: according to the event sent, it will give us the center of the map or the point from which we have sent the event (for example click)
- center: it will ALWAYS give us the center of the map at the time the event was sent.
- map: it ALWAYS gives us the Javascript identifier of the map. It will be the one that we use if we want to apply some change to the map (to add an icon, to add infoWindows, etc).
- bounds: returns the GLatLngBounds (northeast and southwest coordinates) from the map, on the moment that the event has been thrown.
- zoom.
- mapType.
Some events have special arguments. Let's take a look.
Next are the server events with which you will be able to work:
- Click: the event by default, and it will be sent when we click on the map.
- DragEnd: sent when we finished dragging the map with the mouse.
- DragStart: sent when we are beginning to drag the map with the mouse.
- MapTypeChanged: sent when the map view changes. Its argument includes the map type
- MarkerClick: sent when we do click on a marker/icon.
- MoveEnd: sent when a movement of the map is finished. Remember that a movement can be produced by the mouse when dragging, the keyboard, or the "preBuilt" movement control.
- MoveStart: sent when movement of the map starts.
- ServerEvent: we have to manually configurate it and it can gather any type of event we request. We dedicated a special section to it.
- ZoomEnd: sent when we finish making a zoom.Its argument includes the old zoom where we're coming from and the new zoom to which we have gone.
Using the property "ajaxUpdateProgressMessage" you can define the message that will appear while the map is waiting for the server response.
Code.aspx
<cc1:GMap ID="GMap1" runat="server" enableServerEvents="true"
OnMarkerClick="GMap1_MarkerClick"
OnZoomEnd="GMap1_ZoomEnd"
OnMapTypeChanged="GMap1_MapTypeChanged"
OnClick="GMap1_Click"
OnDragEnd="GMap1_DragEnd"
OnDragStart="GMap1_DragStart"
OnMoveEnd="GMap1_MoveEnd"
OnMoveStart="GMap1_MoveStart" />
<div id="messages1"></div>
<div id="messages2"></div>
Code.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GMap1.addControl(new GControl(GControl.preBuilt.LargeMapControl));
GMap1.addControl(new GControl(GControl.preBuilt.MapTypeControl));
}
}
protected string GMap1_Click(object s, GAjaxServerEventArgs e)
{
GMarker marker = new GMarker(e.point);
GInfoWindow window = new GInfoWindow(marker,
string.Format(@"
<b>GLatLngBounds</b><br />
SW = {0}<br/>
NE = {1}
",
e.bounds.getSouthWest().ToString(),
e.bounds.getNorthEast().ToString())
, true);
return window.ToString(e.map);
}
protected string GMap1_MarkerClick(object s, GAjaxServerEventArgs e)
{
return string.Format("alert('MarkerClick: {0} - {1}')", e.point.ToString(), DateTime.Now);
}
protected string GMap1_MoveStart(object s, GAjaxServerEventArgs e)
{
return "document.getElementById('messages1').innerHTML= 'MoveStart at " + e.point.ToString() + " - " + DateTime.Now.ToString() + "';";
}
protected string GMap1_MoveEnd(object s, GAjaxServerEventArgs e)
{
return "document.getElementById('messages2').innerHTML= 'MoveEnd at " + e.point.ToString() + " - " + DateTime.Now.ToString() + "';";
}
protected string GMap1_DragStart(object s, GAjaxServerEventArgs e)
{
GMarker marker = new GMarker(e.point);
GInfoWindow window = new GInfoWindow(marker, "DragStart - " + DateTime.Now.ToString(), false);
return window.ToString(e.map);
}
protected string GMap1_DragEnd(object s, GAjaxServerEventArgs e)
{
GMarker marker = new GMarker(e.point);
GInfoWindow window = new GInfoWindow(marker, "DragEnd - " + DateTime.Now.ToString(), false);
return window.ToString(e.map);
}
protected string GMap1_ZoomEnd(object s, GAjaxServerEventZoomArgs e)
{
return string.Format("alert('oldLevel/newLevel: {0}/{1} - {2}')", e.oldLevel, e.newLevel, DateTime.Now);
}
protected string GMap1_MapTypeChanged(object s, GAjaxServerEventMapArgs e)
{
return string.Format("alert('{0}')", e.mapType.ToString());
}