“lightning: map” component displays a map of one or more locations on google map.
Displaying map in the lightning experience is much easier now, we can use the “lightning: map” component that will plot the map with the address marker in the lightning component. The best thing that we can show more than one address marker on the map.
<lightning:map mapMarkers="{! v.mapMarkers }" | |
center="{! v.centerObj }" | |
zoomLevel="{! v.zoomLevel }" | |
markersTitle="{! v.markersTitle }" | |
showFooter="{ !v.showFooter }" > | |
</lightning:map> |
One more useful attribute –
listView: Use this attribute to display or hides the list of locations that you passed in the mapMarker object. Valid values are visible, hidden, or auto. This value defaults to auto, which shows the list only when multiple markers are present. Passing in an invalid value hides the list view.
For the demonstration purpose, I have used this component (lightning:map) to show all Contacts location on google map for an Account.
Find the component code below:
LightningMapCmp:
<aura:component controller="LightningMapCntrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" > | |
<!-- aura attributes --> | |
<aura:attribute name="mapMarkers" type="Object"/> | |
<aura:attribute name="centerObj" type="Object" /> | |
<aura:attribute name="zoomLevel" type="Integer" /> | |
<aura:attribute name="markersTitle" type="String" /> | |
<aura:attribute name="showFooter" type="Boolean" /> | |
<aura:attribute name="recordId" type="string"/> | |
<aura:attribute name="conObj" type="Contact[]"/> | |
<!-- Init handlers--> | |
<aura:handler name="init" value="{! this }" action="{! c.doInit }"/> | |
<div class="slds"> | |
<div id="container"> | |
<div class="slds-page-header"> | |
CONTACTS ON MAP. | |
</div> | |
<!-- the map component --> | |
<aura:if isTrue="{!v.mapMarkers.length > 0}" > | |
<lightning:map | |
mapMarkers="{! v.mapMarkers }" | |
center="{! v.centerObj }" | |
zoomLevel="{! v.zoomLevel }" | |
markersTitle="{! v.markersTitle }" | |
showFooter="{ !v.showFooter }" > | |
</lightning:map> | |
</aura:if> | |
</div> | |
</div> | |
</aura:component> |
LightningMapCmpController:
({ | |
doInit : function(component, event, helper) { | |
let action = component.get("c.fetchContacts"); | |
action.setParams({ | |
"accountId":component.get("v.recordId") | |
}); | |
action.setCallback(this,function(response){ | |
let state = response.getState(); | |
if(state =='SUCCESS'){ | |
let result = response.getReturnValue(); | |
console.log('Result returned: ' +JSON.stringify(result)); | |
component.set("v.conObj",result); | |
var contacts = component.get("v.conObj"); | |
helper.loadMap(component,event,helper,contacts); | |
}else{ | |
console.log('Something went wrong! '); | |
} | |
}); | |
$A.enqueueAction(action); | |
} | |
}) |
LightningMapCmpHelper:
({ | |
loadMap : function(component,event,helper,contacts) { | |
var mapsArray = []; | |
for(let index=0; index < contacts.length; index++){ | |
var Mobj = { | |
location: { | |
Street: contacts[index].MailingStreet, | |
City: contacts[index].MailingCity, | |
PostalCode: contacts[index].MailingPostalCode, | |
State: contacts[index].MailingState, | |
Country: contacts[index].MailingCountry | |
}, | |
icon: 'standard:contact', | |
title: contacts[index].Name, | |
description: contacts[index].Phone | |
} | |
mapsArray.push(Mobj); | |
} | |
component.set('v.mapMarkers', mapsArray); | |
component.set('v.centerObj', { | |
location: { | |
City: 'Noida' | |
} | |
}); | |
component.set('v.zoomLevel', 12); | |
component.set('v.markersTitle', 'Contacts locations'); | |
component.set('v.showFooter', true); | |
} | |
}) |
LightningMapCmpCSS:
.THIS #container{ | |
border: 1px solid rgb(221, 219, 218); | |
} |
LightningMapCntrl:
public class LightningMapCntrl { | |
@AuraEnabled | |
public static List<Contact> fetchContacts(String accountId){ | |
List<Contact> conList = [Select Id,Name,Phone,MailingCity,MailingStreet,MailingPostalCode,MailingState,MailingCountry from Contact where accountId=:accountId]; | |
return conList; | |
} | |
} |
Note: Add this component on the Account detail page using lightning app builder and make sure that Account record should have one or more contact with ‘Mailing Address’ filled.
🙂
Thank
Arun Kumar
How would I do this to show all accounts locations on a map? Instead just related contacts?
LikeLiked by 1 person
Hello Caroline,
I have written another post to do this please check it out. Hope this will help you! 🙂
https://sfdclesson.com/2020/02/21/locate-accounts-address-on-google-map/
LikeLike
[…] this link : https://sfdclesson.com/2019/07/07/lightningmap-show-contacts-location-on-google-map/ for more […]
LikeLike
This is an excellent post!
I have only two questions, how would i create a search bar to search up locations on the map, and would it be possible to colour code the markers?
LikeLike
Hello,
You can check this post to make the marker colored. https://sfdclesson.com/2021/04/03/spring-21-five-release-highlights/
You can create the search bar but it will be custom. We don’t have anything standard in the map component to use the search bar.
LikeLike
Is there a way to pull the address of the account that you are viewing and make it into a satellite map?
LikeLike