Events
Up until now we have used totally automatic events in various examples. For example; when we click the icon, a window is displayed by calling the "click" event. In the same way, we use the TextualCoordinatesControl when draging the map, the "moveend" event is called capturing the coordinates in the center of the screen and showing it on the textbox.
With GoogleMaps.Subgurim.NET the versatility is absolute, because with the listeners we can configure any kind of events to call our own javascript functions.
To assign events you work with the GListener class. There you have three properties:
-
source: the element that will produce the event. It could be the map itself, an icon, a polyline or a polygon.
-
my_sEvent: the event that will be captured. For example "click", "moveend", "drag", etc...
In order to avoid having to remember all the possible events by heart you can use the enumerator. Event
-
handler:you can create a function on the fly or use a javascript function that we have previously made (In the example both options are explained).
Code.aspx
<script type="text/javascript">>
function alertame()
{
alert('Quedas alertado');
}
</script>
<cc1:GMap ID="GMap1" runat="server" />
Code.aspx.cs
GLatLng latlng = new GLatLng(41, -3.2);
GMap1.setCenter(latlng, 5, GMapType.GTypes.Satellite);
GMarkerOptions mOpts = new GMarkerOptions();
mOpts.draggable = true;
GMarker marker = new GMarker(latlng, mOpts);
GMap1.addGMarker(marker);
GListener listener = new GListener(marker.ID, GListener.Event.dragend, "alertame");
GMap1.addListener(listener);
GMarker mkr = new GMarker();
mkr.options = mOpts;
mkr.javascript_GLatLng = "point";
listener = new GListener(mkr.ID, GListener.Event.dragend, "alertame");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("function(overlay, point) {");
sb.Append("if (overlay){");
sb.Append("alert(overlay.id);");
sb.Append("}");
sb.Append("else{");
sb.Append(mkr.ToString(GMap1.GMap_Id));
sb.Append(listener.ToString());
sb.Append("}");
sb.Append("}");
GListener listener2 = new GListener(GMap1.GMap_Id, GListener.Event.click, sb.ToString());
GMap1.addListener(listener2);
GMap1.addListener(new GListener(GMap1.GMap_Id, GListener.Event.moveend, "function() {alert('I Love Elvis :9P');}"));
GPolygon polygon = new GPolygon();
polygon.Add(latlng + new GLatLng(1, 1));
polygon.Add(latlng + new GLatLng(2, 3));
polygon.Add(latlng + new GLatLng(4, 4));
polygon.close();
GMap1.Add(polygon);
GListener listener3 = new GListener(polygon.PolygonID, GListener.Event.click, "function(point) {alert(point);}");
GMap1.addListener(listener3);