
Hello friends, today I am going to share the build to Display Maps in Lightning Web Component Salesforce.
Also, check this: Get image from rich text field in Salesforce
Check my Blog : https://techdicer.com/
Salesforce provides a lightning-map component that shows one or more locations. Lighting Design System’s map styling is used for this component.
So, it is better to use it.

Key Highlights :
We can show different -2 addresses on Map
We can Zoom -In and Zoom – Out Map.
Can show a marker for location.
Code :
First of all, we create an Apex class to fetch account data. Here I am taking into account records because it has standard address fields.
AccountDataController.cls:
publicclassAccountDataController {
@AuraEnabled(cacheable=true)
publicstaticList<Account> fetchAccounts(){
return[SELECT Id, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry
FROM Account LIMIT 10];
}
}
LWCMapMarker.HTML:
<template>
<!------Header------->
<divclass="slds-tabs_card">
<divclass="slds-page-header">
<divclass="slds-page-header__row">
<divclass="slds-page-header__col-title">
<divclass="slds-media">
<divclass="slds-media__figure">
<spanclass="slds-icon_container slds-icon-standard-opportunity">
<lightning-iconicon-name="standard:recipe"alternative-text="recipe"title="recipe"></lightning-icon>
</span>
</div>
<divclass="slds-media__body">
<divclass="slds-page-header__name">
<divclass="slds-page-header__name-title">
<h1>
<span>Display Maps in Lightning Web Component Salesforce</span>
<spanclass="slds-page-header__title slds-truncate"title="Recently Viewed">TechDicer</span>
</h1>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div> <br/>
<!-- create card -->
<lightning-cardvariant="Narrow"title="Salesforcer Map"icon-name="standard:location">
<divclass="slds-var-p-around_small">
<lightning-mapmap-markers={mapMarkers} markers-title={markersTitle} zoom-level={zoomLevel}>
</lightning-map>
</div>
</lightning-card>
</template>
LWCMapMarker.JS:
import{ LightningElement, wire, track } from 'lwc';
importfetchAccounts from '@salesforce/apex/AccountDataController.fetchAccounts';
exportdefaultclassLWCMapMarker extendsLightningElement {
@track error;
@track mapMarkers = [];
@track markersTitle = 'Accounts';
@track zoomLevel = 4;
/ Load address from Controller /
@wire(fetchAccounts, {})
wireAccount({ error, data }) {
if(data) {
data.forEach(dataItem => {
this.mapMarkers = [...this.mapMarkers,
{
location: {
City: dataItem.BillingCity,
Country: dataItem.BillingCountry,
},
icon: 'custom:custom26',
title: dataItem.Name,
}
];
});
this.error = undefined;
} elseif(error) {
this.error = error;
}
}
}
