lightning:map component
lightning:map introduced in winter 19 which can be use display a map of one or more locations using Google Maps. We can pass markers to the component to define the locations to map. A marker can be a coordinate pair of latitude and longitude, or a set of address elements: City, Country, PostalCode, State, and Street.This component requires API version 44.0 and later. lightning:map has required attribute mapMarkers which accepts array of markers that indicate location. A market contains
- Location information: This can be a coordinate pair of latitude and longitude, or an address composed of address elements.
- Descriptive information: This is information like title, description and an icon which is information relevant to the marker but not specifically related to location.
Example
Create a new Object called 'Tower' with two new custom fields
1. Field Label: State
Type: Master-Detail (Account)
2. Field Label: Location
Type: GeoLocation (Latitude & Longitude Display Notation: Decimal )
Once metadata is ready then create some Tower records with Latitude & Longitude values.
Now copy & paste below code to show Map with markers.
UtilityClass.cls
TowerController.cls
TowerMap.cmp
TowerMapController.js
TowerMapHelper.js
Create a new Object called 'Tower' with two new custom fields
1. Field Label: State
Type: Master-Detail (Account)
2. Field Label: Location
Type: GeoLocation (Latitude & Longitude Display Notation: Decimal )
Once metadata is ready then create some Tower records with Latitude & Longitude values.
Now copy & paste below code to show Map with markers.
UtilityClass.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UtilityClass { | |
public static List<sObject> queryObjects(String theObject, List<String> theFields, List<String> theFilters, String sortField, String sortOrder) { | |
String theQuery = 'SELECT ' + string.join(theFields, ','); | |
theQuery += ' FROM ' + theObject; | |
boolean firstFilter = true; | |
for(String filter : theFilters) { | |
String clauseToUse = (firstFilter) ? ' WHERE ' : ' AND '; | |
filter = filter.trim(); | |
filter = filter.replaceAll('(\\s+)', ' '); // removing white spaces | |
theQuery += clauseToUse + filter; | |
} | |
if(!String.isEmpty(sortField)) { | |
theQuery += ' ORDER BY ' + sortField; | |
if(!String.isEmpty(sortOrder)) { | |
theQuery += ' ' + sortOrder; | |
} | |
} | |
String queryResult = string.escapeSingleQuotes(theQuery); | |
return Database.query(queryResult); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TowerMapController { | |
@AuraEnabled | |
public static List<Tower__c> getAllTowers() { | |
String theObject = 'Tower__c'; | |
List<String> theFields = new List<String>{'Id', 'Name', 'State__r.Name', 'Location__Latitude__s', 'Location__Longitude__s'}; | |
List<String> theFilters = new List<String>(); | |
String sortField = 'Name'; | |
String sortOrder = 'ASC'; | |
List<Tower__c> allTowers = UtilityClass.queryObjects(theObject, theFields, theFilters, sortField, sortOrder); | |
return allTowers; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component controller="TowerMapController" implements="force:appHostable,flexipage:availableForAllPageTypes">> | |
<aura:attribute name="mapMarkers" type="object" access="private"/> | |
<aura:attribute name="markersTitle" type="object" access="private"/> | |
<aura:handler name="init" value="{!this}" action="{!c.init}"/> | |
<aura:if isTrue="{!!empty(v.mapMarkers)}"> | |
<lightning:map mapMarkers="{!v.mapMarkers}" zoomLevel="10" markersTitle="{!v.markersTitle}"/> | |
</aura:if> | |
</aura:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
init : function(component, event, helper) { | |
helper.initHelper(component, event, helper); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
initHelper : function(component, event, helper) { | |
helper.utilSetMarkers(component, event, helper); | |
}, | |
utilSetMarkers: function(component, event, helper){ | |
//Variables declared with the let keyword can have Block Scope. | |
//{ let x = 2; } x cannot be uset outside the block | |
var action = component.get("c.getAllTowers"); | |
action.setCallback(this, function(response){ | |
var state = response.getState(); | |
if(state == 'SUCCESS') { | |
const data = response.getReturnValue(); | |
const dataSize = data.length; | |
const markers = []; | |
for(let index = 0; index < dataSize-1; index++) { | |
const Tower = data[index]; | |
markers.push({ | |
'location': { | |
'Latitude' : Tower.Location__Latitude__s, | |
'Longitude' : Tower.Location__Longitude__s | |
}, | |
'icon': 'utility:Tower', | |
'title' : Tower.Name, | |
'description' : Tower.Name + ' Tower Location at ' + Tower.State__r.Name | |
}); | |
} | |
component.set("v.markersTitle", "Out and About Communications Tower Locations"); | |
component.set("v.mapMarkers", markers); | |
} | |
}); | |
$A.enqueueAction(action); | |
} | |
}) |