+ Start a Discussion
Naveen SanaNaveen Sana 

How to get the location of logged in user using apex

Hi, 

How can we achieve the region details of the logged-in user by using apex?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Naveen,

Greetings to you!

You need to use Geolocation API (https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API) or Google's GeoCoding API (https://developers.google.com/maps/documentation/geocoding/intro). Please refer to the below links which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/138429/how-to-get-current-user-geo-locationlongitude-latitude

https://developer.salesforce.com/forums/?id=9060G0000005dHlQAI

https://developer.salesforce.com/forums/?id=906F0000000A372IAC

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Naveen,

You can get the current location of logged user. In this code get loaction in latitude and longitude format.
Please try the code given below.
 
<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>

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Naveen SanaNaveen Sana
Hi Khan,

thanks for your reply!!

actually my requirement is to get the weather report based on the location of the logged in user on loading of aura componet.
 
Naveen SanaNaveen Sana
Hi Deepali,

I tried the same type of code which you have sent, but no luck !!

I am getting this output --> Unable to retrieve your location
getloc2: function geoFindMe() {        
          if (!navigator.geolocation){
            console.log('Geolocation is not supported by your browser');
            return;
          }
          function success(position) {
            var latitude  = position.coords.latitude;
            var longitude = position.coords.longitude;
            console.log(latitude);
            console.log(longitude);
          };
          function error() {
            console.log('Unable to retrieve your location');
          };        
          navigator.geolocation.getCurrentPosition(success, error);
        }
Tad Aalgaard 3Tad Aalgaard 3

The code you posted uses code that when run will pop up an alert to the user asking them if they will allow the browser to know their location. Is that popping up for you so that you can click "Allow"?