The best way to experiment with a new component framework is to create components, and a simple Map component is often one of the first candidates that come to mind.
In this article, I share a Lightning Map Component built with the popular Leaflet open-source library.


This component also provides an example of loading external Javascript libraries and CSS stylesheets using the ltng:require tag.
Step 1: Import Leaflet as a Static Resource
- Download the latest stable version of leaflet here (0.7.3 at the time of this writing).
- In Setup, go to Build > Develop > Static Resources, and click New.
- Specify leaflet (all lowercase) as the Name, click the Choose File button and select the leaflet zip file you just downloaded.
- Click Save.
Step 2: Create the Component
- In the Developer Console, click File > New > Lightning Component. Specify MyMap as the bundle name and click Submit
- Implement the Component as follows:
1<aura:component implements="force:appHostable"> 2 3 <ltng:require styles="/resource/leaflet/leaflet.css" /> 4 <ltng:require scripts="/resource/leaflet/leaflet.js" 5 afterScriptsLoaded="{!c.jsLoaded}" /> 6 7 8 9</aura:component> - Click File > Save to save the file
- Click CONTROLLER (upper right corner of the code editor), and implement the controller as follows:
1({ 2jsLoaded: function(component, event, helper) { 3 setTimeout(function() { 4 var map = L.map('map', {zoomControl: false}).setView([37.784173, -122.401557], 14); 5 L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', 6 { 7 attribution: 'Tiles © Esri' 8 }).addTo(map); 9 10 // Add marker 11 L.marker([37.784173, -122.401557]).addTo(map) 12 .bindPopup('Home of Dreamforce'); 13 }); 14} 15}) - Click File > Save to save the file
- Click STYLE (upper right corner of the code editor), and implement the following style:
1.THIS.map { 2 position: absolute; 3 top: 0; 4 left: 0; 5 right: 0; 6 bottom: 0; 7} - Click File > Save to save the file
Step 3: Deploy the Component in the Salesforce1 App
To add a Tab for the Lightning Component:
- In Setup, click Create > Tabs.
- Click New in the Lightning Component Tabs section.
- Select c:MyMap as the Lightning Component.
- Specify My Map as the Tab Label and My_Map as the Tab Name.
- Click the magnifier icon and select Lightning as the tab icon.
- Click Next and Save.
To add the Tab to the Salesforce1 App Menu:
- In Setup, Select Administer > Mobile Administration > Mobile Navigation.
- Select My Map in the Available list.
- Click Add.
- Select My Map in the Selected List, and click the Up button to move the tab up in the menu order.
- Click Save.
You can now test the component in the Salesforce1 app on your device. Alternatively, you can preview the component in the Salesforce1 app simulator:
- In your browser, remove the part of the URL that comes immediately after salesforce.com.
- Append /one/one.app to the URL immediately after salesforce.com and press Enter: this starts the Salesforce1 Application simulator.
- Click the menu button in the upper left corner.
- Select My Map in the menu.
- Preview the component.
