How to get Current Position Latitude and Longitude of Google Map in LWC | How to retrieve current geolocation (navigator.geolocation) fields of google map and display on custom Lightning Page in LWC

549 views

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 β†’

How to get Current Position Latitude and Longitude of Google Map in LWC -- w3web.net
 

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]

  1.    <template>
  2.     <lightning-card title="How to Get My Current Location On Google Map in LWC">
  3.  
  4.         <div class="slds-p-around_small">
  5.  
  6.             <lightning-map map-markers={markerVar} zoom-level="10"  style="height: 350px;"></lightning-map>
  7.         </div>
  8.     </lightning-card>
  9.  
  10.  
  11. </template>

 

Create Lightning Web Component JavaScript β†’

Step 2:- Create Lightning Web Component : currentLocationLwc.js

 
βœͺ The Big Announcement: -
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]

  1.  
  2.   import { LightningElement, track } from 'lwc';
  3.  
  4. export default class CurrentLocationLwc extends LightningElement {
  5.     markerVar = [];
  6.     //lstMarkers
  7.  
  8.     constructor() {
  9.         super();
  10.         console.log('get current location in constructor');
  11.        this.myCurrentLocationOnGoogleMap();
  12.  
  13.     }
  14.  
  15.  
  16.     myCurrentLocationOnGoogleMap(){
  17.         if (navigator.geolocation) {
  18.             navigator.geolocation.getCurrentPosition(position => {             
  19.                 var latVal = position.coords.latitude;
  20.                 var longVal = position.coords.longitude;
  21.                 this.markerVar = [{
  22.                     location : {
  23.                         Latitude: latVal,
  24.                         Longitude : longVal
  25.                     },
  26.                     title : 'Your Current Location are here'
  27.                 }];
  28.             });
  29.         }
  30.     }
  31.  
  32. }

 

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]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets> 
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.     </targets>
  10. </LightningComponentBundle>

 

 

Further post that would you like to learn in Salesforce

 
 

 

FAQ (Frequently Asked Questions)

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

 
   
 
 

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment