Anonymous user  |  Log in  |  Create Account

Server events

With server events, you will be able to travel from the map to the server using 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's the source of 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 (properties available for all events):
    • who: it's the same as the source previously described. It indicates who has sent the event.
    • point: according to the event sent, it will contain the center of the map or the point where the event occurred (for example click)
    • center: it will ALWAYS be the center of the map at the time the event was sent.
    • map: it ALWAYS be the Javascript identifier of the map. It will be the one that will be used to apply a 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 may have special arguments. Let's take a look.

Next are the server events than can be handled:

  • 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.
  • PolygonClick: sent when we do click on a polygon.
  • PolylineClick: sent when we do click on a polyline.
  • 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 configure it and it can gather any type of event we request. There's a dedicated example to this.
  • 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.
  • MapLoad: sent when the map is loaded.

Using the property "ajaxUpdateProgressMessage" the message that will appear while the map is waiting for the server response can be defined.

<
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
private void BuildMap()
{
    GMap1.Add(new GControl(GControl.preBuilt.LargeMapControl));
    GMap1.Add(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(),
                                             e.bounds.getNorthEast()),
                                         true);

    return window.ToString(e.map);
}

protected string GMap1_MarkerClick(object s, GAjaxServerEventArgs e)
{
    return string.Format("alert('MarkerClick: {0} - {2} - {1}')", e.point, DateTime.Now, e.map);
}

protected string GMap1_MoveStart(object s, GAjaxServerEventArgs e)
{
    return "document.getElementById('messages1').innerHTML= 'MoveStart at " + e.point + " - " + DateTime.Now + "';";
}

protected string GMap1_MoveEnd(object s, GAjaxServerEventArgs e)
{
    return "document.getElementById('messages2').innerHTML= 'MoveEnd at " + e.point + " - " + DateTime.Now + "';";
}

protected string GMap1_DragStart(object s, GAjaxServerEventArgs e)
{
    GMarker marker = new GMarker(e.point);
    GInfoWindow window = new GInfoWindow(marker, "DragStart - " + DateTime.Now, 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, 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());
}

protected string GMap1_ServerEvent(object s, GAjaxServerEventOtherArgs e)
{
    return string.Format("alert('{0} - {1} - {2} - {3}')", e.point, e.eventName, e.eventArgs, DateTime.Now);
}
Powered by Subgurim.NET