function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rocks_SFDCRocks_SFDC 

How to get the Latitude and Longitude of a Current Location

Hi Team, 

 

How can we get the lat and long of a current address automatically by using apex code.

 

Do we have any standard methods to retrieve those valuse.

 

Thanks,

Anil

Vinita_SFDCVinita_SFDC

Hi Anil,

 

Refer section 'Accessing Geolocation via Apex and Visualforce' in following link, which explains with code snippets:

 

http://blogs.developerforce.com/developer-relations/2012/10/winter-13-using-apex-and-soql-with-geolocation.html

Rocks_SFDCRocks_SFDC

Hi Vinita,

 

Thank you for your response.

 

How can we get the current location coordinates by using apex code.

 

Thanks,

Anil

bob_buzzardbob_buzzard

You can't, because Apex runs on the Salesforce server and doesn't know where you are.  You have to do it via the user's device using javascript.  There's an example in the link posted above for Visualforce, I guess you could also do this via a sidebar component if that made any sense for your scenario.

Vinita_SFDCVinita_SFDC

Hi,

If you define current location in a field with type geolocation, then you can query on this field in your apex code for the cordinates  Location__latitude__s and Location__longitude__s.

Ex: SELECT  Location__latitude__s, Location__longitude__s, name FROM Account where Location_c='abc';

Here location field is of type geolocation.

Mike ArthurMike Arthur
If you reference the lat and long from javascript, e.g. in Aura component or LWC, remember that javascript is Case Sensitive, so use

Location__Latitude__s
Location__Longitude__s

Don't ask how long I spent debugging that one :-)