3

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

2
  • 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 '16 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 '17 at 14:40
6

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

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

<div id="startLat">
</div>

<div id="startLon">
</div>

<script>
    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;
        };
        navigator.geolocation.getCurrentPosition(geoSuccess);
    };
</script>
</apex:page>

The output would be

enter image description here

0

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.

https://developers.google.com/maps/documentation/geocoding/intro

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