Locate Accounts Address On Google Map

AccountsOnMap:

<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="accObj" type="Account[]"/>
<!-- Init handlers-->
<aura:handler name="init" value="{! this }" action="{! c.doInit }"/>
<div class="slds">
<div id="container">
<div class="slds-page-header">
ACCOUNTS 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 }"
listView="auto">
</lightning:map>
</aura:if>
</div>
</div>
</aura:component>
view raw AccountsOnMap hosted with ❤ by GitHub

AccountOnMapController:

({
doInit : function(component, event, helper) {
let action = component.get("c.fetchAllAccounts");
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.accObj",result);
var accounts = component.get("v.accObj");
helper.loadMap(component,event,helper,accounts);
}else{
console.log('Something went wrong! ');
}
});
$A.enqueueAction(action);
}
})

AccountOnMapHelper:

({
loadMap : function(component,event,helper,account) {
var mapsArray = [];
for(let index=0; index < account.length; index++){
var Mobj = {
location: {
Street: account[index].BillingStreet,
City: account[index].BillingCity,
PostalCode: account[index].BillingPostalCode,
State: account[index].BillingState,
Country: account[index].BillingCountry
},
icon: 'standard:account',
title: account[index].Name,
description: account[index].Website
}
mapsArray.push(Mobj);
}
component.set('v.mapMarkers', mapsArray);
component.set('v.centerObj', {
location: {
City: 'Noida'
}
});
component.set('v.zoomLevel', 12);
component.set('v.markersTitle', 'Accounts locations');
component.set('v.showFooter', true);
}
})

AccountsOnMapCSS:

.THIS #container{
border: 1px solid rgb(221, 219, 218);
}

Apex Class:

public class LightningMapCntrl {
@AuraEnabled
public static List<Account> fetchAllAccounts(){
List<Account> accList = [Select Id,Name,Website,BillingCity,BillingStreet,BillingPostalCode,BillingState,BillingCountry from Account ];
return accList;
}
}

Create a lightning component tab and use this component in the tab.

Follow this link : https://sfdclesson.com/2019/07/07/lightningmap-show-contacts-location-on-google-map/ for more details.

Thanks

Arun Kumar

Arun Kumar

Arun Kumar is a Salesforce Certified Platform Developer I with over 7+ years of experience working on the Salesforce platform. He specializes in developing custom applications, integrations, and reports to help customers streamline their business processes. Arun is passionate about helping businesses leverage the power of Salesforce to achieve their goals.

This Post Has 2 Comments

  1. Chandra Sekhar

    Hey Arun Kumar thanks for sharing the lightning component.
    How can I build the functionality which will find me the nearest 10 accounts or 10 opportunity. How can I leverage this functionality.
    Thanks

  2. sai shanmukh

    Hi Arun Kumar, Thanks for the details. How to plot the data as per the list view records. Please revert.

Leave a Reply