Search
Handling Events

The mapping library raises many events that let you know the actions the user has performed on the map or the state of the map. The MapView control raises events that notify that all its layers are loaded - layerLoad or that the component itself is loaded and ready for interaction - viewLoad.

You handle any event by wiring a delegate using the  addEventListener method this way:

JavaScript  Copy Code

var m = MindFusion.Mapping;

// create a new instance of the mapView
view = new m.MapView(document.getElementById("mapView"));

var l = new m.MapLayer("Streets");
l.urlTemplate = "https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/{z}/{x}/{y}?access_token=YOUR_ACCESS_KEY";

l.attribution = 'Map tiles by <a href="https://www.mapbox.com/about/maps/">&copy; Mapbox</a>,<a href="http://www.openstreetmap.org/about/">&copy; OpenStreetMap</a> <a href="https://www.mapbox.com/map-feedback/#/-74.5/40/10"><code>Improve this map</</a>';
view.layers.add(l);

view.viewLoad.addEventListener ( function (sender, args )
{
    view.activeLayer = l;
});

The decorationClick event lets you know when the user has clicked on a Decoration while decorationHover is raised when they move the mouse over a Decoration. The click event is raised when the user clicks enywhere on a MapView, not just over a Decoration.

The decorations and layers properties of the MapView are of type ObservableCollection. Each ObservableCollection raises events when it's been changing or has changed. These are collectionChanging and collectionChanged. The collectionChanging event lets you cancel the action:

JavaScript  Copy Code

var m = MindFusion.Mapping;

var markers = new m.DecorationLayer("Images");

// create some markers with images
var mark = new m.Marker(new m.LatLong(-22.951916, -43.210487));
mark.imageSrc = "./images/christ_redeemer.png";
mark.text = "Christ the Redeemer";
markers.decorations.add(mark);

markers.decorations.collectionChanging.addEventListener(handleCollectionChanging);

function (handleCollectionChanging) (sender, args )
{
     if(args.action == m.NotifyCollectionChangedAction.Add)
     {
          args.cancel = true;
     }
}