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
AK-2AK-2 

My VF page can't get geolocation in Salesforce1

I have a VF page that's a simple proof of concept to get geolocation. The page is accessible through Salesforce1. The javascript on the page, makes a call to navigator.geolocation.getCurrentPosition and shows the location on the page. The page works correctly on desktop but through salesforce1, the geolocation.getCurrentPosition doesn't go go to success or error function handlers. I added javascript alerts to try to debug the problem. When viewing the page through salesforce1 I see the alert message "geolocation enabled" that I added in the if (navigator.geolocation) but none of the alerts from the success handler or the error handler show up! (Below is my VF page code)

What am I doing wrong?

Thank you. 
 
<apex:page controller="testController" doctype="html-5.0">
  
   <label id='test'>No geo-location</label>
   
   <script type="text/javascript">
  
    //error handler
    function showError(error) {
    alert('in error');
    
    var testElem = document.getElementById("test");
    var strError = "Unknown Error";
    switch(error.code) {
        case error.PERMISSION_DENIED:
            strError = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            strError = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            strError = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            strError = "An unknown error occurred."
            break;
    }
    alert(strError);
    testElem.innerText = strError;
    };
    
    //success handler
    function successGeo(position){
            
            lat = position.coords.latitude;
            lon = position.coords.longitude;
            var testElem = document.getElementById("test");
            var strLatLong = 'lat: ' + lat.toString() + ', long:' + lon.toString();
            alert('Location: ' + strLatLong);
            testElem.innerText  = strLatLong;
    };
    
    // detection capabilities with JavaScript
    if (navigator.geolocation) {
        var testElem = document.getElementById("test");
        testElem.innerText = 'getting geo location!';
        alert('geolocation enabled1');
        
        navigator.geolocation.getCurrentPosition(successGeo, showError);
    };
    
    
   </script>





 
Best Answer chosen by AK-2
AK-2AK-2
Aha, got it! I was wondering why Salesforce1 wasn't prompting me for permission to access location. I googled about Salesforce1 and location services and came across another page here: http://support.arrowpointe.com/geopointe-mobile-for-salesforce1-setup/ which pointed me to this known issue: https://success.salesforce.com/issues_view?id=a1p30000000T5l6AAC

Apparently, there is a bug in Salesforce1 and it doesn't ask for permission to access location and that's why the location service was not available. There is a workaround mentioned in the issue by Salesforce and that worked for me.
I am all happy now :-)

All Answers

SarfarajSarfaraj
If you are using Salesforce 1 from browser it should work fine. 
If you are using in some mobile device you have to turn on location settings. 

For IOS check,
https://support.google.com/coordinate/answer/2800543?hl=en
For Android,
https://support.google.com/coordinate/answer/2569281?hl=en

Please let me know if this solves your issue.

--Akram
AK-2AK-2
Thanks for the reply Akram. I am using the Salesforce1 app on iPhone. I checked the iphone settings and the location services as well as wifi are both on.
Not sure what I am missing.
SarfarajSarfaraj
Some implementation of geolocation has some limitations. I think it is trying to get the geolocation and waiting infinitely. Also some does not use GPS without the enableHighAccuracy option. Check these links. Try setting some timeout and force it to use GPS.
https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation.getCurrentPosition

--Akram
AK-2AK-2
Thanks again Akram. Unfortunately, setting enableHighAccuracy option to true didn't help. Adding a 10 second timeout didn't help either as I simply got the error your request has timed out. I still can't figure out why the page is not able to get the geolocation to work :-(
AK-2AK-2
By the way, one additional bit of info. When I found another location page which simply grabs the location that someone else had put together (http://test.bemoko.com/test/iphonegeo).
When I open this url on my phone, I get a prompt that says the page would like to use your current location and I have to hit "OK" to let the page use my location. I don't get any such prompt when I use the VF page in Salesforce1 neither do I get that prompt when I start Salesforce1. I am guessing since Salesforce1 doesn't have permission to use location, the VF page is hanging up. I just don't know how to let Salesforce1 use geolocation!
AK-2AK-2
Aha, got it! I was wondering why Salesforce1 wasn't prompting me for permission to access location. I googled about Salesforce1 and location services and came across another page here: http://support.arrowpointe.com/geopointe-mobile-for-salesforce1-setup/ which pointed me to this known issue: https://success.salesforce.com/issues_view?id=a1p30000000T5l6AAC

Apparently, there is a bug in Salesforce1 and it doesn't ask for permission to access location and that's why the location service was not available. There is a workaround mentioned in the issue by Salesforce and that worked for me.
I am all happy now :-)
This was selected as the best answer
SarfarajSarfaraj
Glad that you found the solution. Good job :)
AK-2AK-2
Thanks for your help Akram!
Gautam Manchanda 12Gautam Manchanda 12
Hey AK-2,

I am facing the same issue now. Were u able to resolve this earlier?

Thanks,
Gautam Manchanda