To add an event listener on the map call the AddListener method of the map object like this:
event |
String Required. The event you want to track. |
callback |
String Required. The function to be called when the event is fired. This is the string name of the function. |
Return: Listener object
Make sure that all the listener callbacks must be accessiblle globally. Listener callback defined within a function throws an error saying "Function `callback` is not defined".
app.LoadPlugin("MapView"); var marker; function OnStart() { lay = app.CreateLayout("Linear", "VCenter, FillXY"); var apiKey = "AIzaSyD2eHntKEmh272p3ac6YjWPbPkwo3m2mck"; map = app.CreateMapView(apiKey, 0.9, 0.5, 37.775, -122.434, 13); map.SetOnReady(MapOnReady); lay.AddChild(map); app.AddLayout(lay); } function MapOnReady() { map.AddListener("click", "OnMapClick"); } function OnMapClick(ev) { var data = JSON.parse(ev); var pos = data.latLng; map.CreateMarker(pos.lat, pos.lng); }
app.LoadPlugin("MapView"); var marker; function OnStart() { lay = app.CreateLayout("Linear", "VCenter, FillXY"); var apiKey = "AIzaSyD2eHntKEmh272p3ac6YjWPbPkwo3m2mck"; map = app.CreateMapView(apiKey, 0.9, 0.5, 37.775, -122.434, 13); map.SetOnReady(MapOnReady); lay.AddChild(map); app.AddLayout(lay); } function MapOnReady() { map.AddListener("dragstart", "OnDragStart"); map.AddListener("dragend", "OnDragEnd"); } function OnDragStart(ev) { app.ShowPopup("Start: "+ev); } function OnDragEnd(ev) { app.ShowPopup("End: "+ev); }