I have to find the currently logged in users Geo-Location (longitude, latitude) in apex code and java script. How to fetch Geo-Location ?

  • Using geolocation has nothing to do with Salesforce. This question is off topic and should instead be asked on Stack Overflow.
    – Adrian Larson
    Aug 26, 2016 at 10:10
  • Did you get the required solution ? I am looking to achieve the same, so that i can call the apex on a record creation and save the latitude and longitude to the custom fields on the newly created record.
    – Sid
    May 23, 2017 at 14:40

2 Answers 2


Well I have a sample VF page that shows your Latitude and Longitude. You can refer that.

<!-- Begin Default Content REMOVE THIS -->
<h1>Welcome </h1> Your Location is

<div id="startLat">

<div id="startLon">

    window.onload = function() {
        var startPos;
        var geoSuccess = function(position) {
            startPos = position;
            document.getElementById('startLat').innerHTML = startPos.coords.latitude;
            document.getElementById('startLon').innerHTML = startPos.coords.longitude;

The output would be

enter image description here


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.


Not the answer you're looking for? Browse other questions tagged or ask your own question.