Hey guys, today in this post we are going to learn about How to retrieve current Geolocation (navigator.geolocation) fields of google map and display on custom Lightning Page in LWC.
How to display google map using lightning-map element in salesforce lightning web component LWC, To know more details, Click Here.
Final Output →
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps â–¾
Create Lightning Web Component HTML →
Step 1:- Create Lightning Web Component : currentLocationLwc.html
SFDX:Lightning Web Component >> New >> currentLocationLwc.html
currentLocationLwc.html [Lightning Web Component HTML]
<template>
<lightning-card title="How to Get My Current Location On Google Map in LWC">
<div class="slds-p-around_small">
<lightning-map map-markers={markerVar} zoom-level="10" style="height: 350px;"></lightning-map>
</div>
</lightning-card>
</template>
Create Lightning Web Component JavaScript →
Step 2:- Create Lightning Web Component : currentLocationLwc.js
As per student's demands, I re-opened my eBook of "Salesforce Tutorial" Limited-time huge discount offer Absolutely 50% Off.
I am thinking to give you discount offer occasion of Gandhi Jayanti for a powerful "Salesforce admin course" where you can Understand from Basic Concepts to advanced label in Salesforce.
👉 So Don't MISS it... (Access Right Now)
👉 Get Huge Discount Offer 50%: - Get eBook
SFDX:Lightning Web Component >> New >> currentLocationLwc.js
currentLocationLwc.js [LWC JavaScript File]
import { LightningElement, track } from 'lwc';
export default class CurrentLocationLwc extends LightningElement {
markerVar = [];
//lstMarkers
constructor() {
super();
console.log('get current location in constructor');
this.myCurrentLocationOnGoogleMap();
}
myCurrentLocationOnGoogleMap(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
var latVal = position.coords.latitude;
var longVal = position.coords.longitude;
this.markerVar = [{
location : {
Latitude: latVal,
Longitude : longVal
},
title : 'Your Current Location are here'
}];
});
}
}
}
Create Lightning Web Component Meta XML →
Step 3:- Create Lightning Web Component : currentLocationLwc.js-meta.xml
SFDX:Lightning Web Component >> New >> currentLocationLwc.js-meta.xml
currentLocationLwc.js-meta.xml [LWC Meta Data XML]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Further post that would you like to learn in Salesforce
How to get current location latitude and longitude in Salesforce?
You need to use Google's GeoCoding API. Make a http request to the API with the required parameters and you would get the lat long as the response. More details about the API and examples are here.
What is the difference between @track and @API in LWC?
@api: It is used to expose a variable or functions publically and make properties reactive. @track: It is used to make variable private but reactive. Tracked properties are also called private reactive properties. @wire: To read Salesforce data, Lightning web components use a reactive wire service.
How do I find a location with latitude and longitude?
To find a location using its latitude and longitude on any device, just open Google Maps. On your phone or tablet, start the Google Maps app. On a computer, go to Google Maps in a browser. Then enter the latitude and longitude values in the search field — the same one you would ordinarily use to enter an address.
Related Topics | You May Also Like
- My Udemy Course →



