Skip to content

Leaflet module events

The Leaflet module comes with an event system where you can add functions that will be called when something happens on the map. To setup a new hook on an event, you have to call Leaflet.on() with the unique id of your map, the event and your callback function. Additional parameters are in a list passed as the first argument to the function. Here is an example:

Leaflet.on('myMapId', 'onDidClickMap', function(data){
    cat(paste0('User clicked on the map on lat:', data$lat, ' and lng:', data$lng, '.'));
});

You can also use a previously created function:

myCallback <- function(data){
    cat(paste0('User clicked on the map on lat:', data$lat, ' and lng:', data$lng, '.'));
}
Leaflet.on('myMapId', 'onDidClickMap', myCallback);

onDidChangeView

Called when the user has finished to move or zoom the map, and its view has changed. The data parameter contains:

  • northLat: the latitude of the most north position of the current view;
  • eastLng: the longitude of the most east position of the current view;
  • southLat: the latitude of the most south position of the current view;
  • westLng: the longitude of the most west position of the current view;
  • zoomLevel: the current zoom level of the map.

onDidClickMap

Called when the user clicked somewhere on the map. The data parameter contains:

  • lat: the latitude of the click;
  • lng: the longitude of the click.

onDidClickShape

Called when the user clicked on a shape previously added with addCircle() or addPolygon(). The data parameter contains:

  • shapeId: the zone unique id of the shape;
  • northLat, eastLng, southLat, westLng and zoomLevel: same as the onDidChangeView event.

onDidClickZone

Called when the user clicked on a zone previously added with addGeoJSON(). The data parameter contains:

  • zoneId: the zone unique id as defined in addGeoJSON();
  • northLat, eastLng, southLat, westLng and zoomLevel: same as the onDidChangeView event.

onDidLoad

onDidLoad is called when the map finished its initialization, and is ready to draw shapes, set view and any other uses. There is no argument to onDidLoad.