Leaflet module usage
Creating the map
Create a new empty label widget in a GUI and give it a unique id, like myMap
.
Create a new R file and add it to the sequencer before the GUI containing the map.
In the R file, source the module:
You can then initialize the map. This must be done before entering the GUI with the map.
Leaflet.createMap(
'myMapId',
rpgm.step('main', 'myGUI'),
'myMap',
layer = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
height = 512,
options = list(
center=Leaflet.latLng(48, 2),
zoom=5
),
layerOptions = list(
maxZoom=19,
attribution='© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
)
)
See the Leaflet.createMap()
section for more information on the parameters. You then have access to all the functions for controlling and drawing
on the map, always by using your myMapId
id for the mapId
functions. You can setup event listener to change the behaviour of the map depending
on the user's inputs (see Events).
Icons and markers
You can create custom icons to show on the map. Custom icons images must be in the output directory to work.
The module comes with a default icon located in leaflet/resources/icon.png
.
The first step is to copy the file to the output directory, then create the icon with a custom id. This custom id
is used as the iconId
parameter in Leaflet.marker()
. Here is a complete example:
file.copy(rpgm.pgmFilePath('modules/leaflet/resources/icon.png'), rpgm.outputFile("leaflet_icon.png"));
leafletIconPath <- if(rpgm.isServer()) rpgm.outputFileURL('leaflet_icon.png') else rpgm.outputFile('leaflet_icon.png')
Leaflet.createIcon('defaultIcon', list(
iconUrl = leafletIconPath,
iconSize = c(48, 48),
iconAnchor = c(24, 48),
popupAnchor = c(0, -48)
));
Leaflet.marker('defaultIcon', Leaflet.latLng(48,2), 'My popup text')