3

I have a function triggered by button click that checks geolocation..it works fine on phones when geolocation is on, and when off, a message saying so displays, as expected. The problem occurs when first the phone's location service is turned off, the button is clicked(message pops up, as expected), then if the user turns location services back on while app is still open, and again clicks the button, still the same 'no location service' message pops up.

Is there a way to check if the phone's location service is turned on or off on each button click? Getting the same results on Android and IOS.

code:

$(document).ready(function () {

    $('#smallScreenGeolocate').on('click', function(){

     getCurrentLocation();
     });
});

function getCurrentLocation () {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(addGeolocationMarker, locationError);
    return true;
}
else {
    alert("Browser doesn't support Geolocation. Visit http://caniuse.com to discover browser support for the Geolocation API.");
    return false;
}
}
JasonBK
  • 539
  • 3
  • 12
  • 37
  • no one? I cannot find anything to fix this...seems odd that I can't detect whether geolocation has ben turned on or off while a webapp is open...? – JasonBK Jun 27 '16 at 20:48
  • are we talking about some kind of framework? like phonegap, cordova, xamarin, or a simple web application? – ddb Jul 04 '16 at 13:36
  • if you refresh the page (with location active), then location details are accessible? – ddb Jul 04 '16 at 14:03
  • @ddb yes,.. i am not actually developing the wrapper for the webapp but I beleive they are using xamarin...but I have that problem with the webapp itself; I need to refresh the page for geolocation to be detected. – JasonBK Jul 04 '16 at 21:51

1 Answers1

1

Check this answer from another SO post https://stackoverflow.com/a/14862073/6539349

You have to check what was the error as suggested here http://www.w3schools.com/html/html5_geolocation.asp

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
   x.innerHTML = "Latitude: " + position.coords.latitude +
   "<br>Longitude: " + position.coords.longitude;
}

The second parameter showError of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user's location:

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation."
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable."
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out."
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred."
            break;
    }
}
Community
  • 1
  • 1
ebyt
  • 312
  • 1
  • 9
  • does the app shows one of those errors when tap on the button after switchin on localisation? if yes, which one? – ddb Jul 04 '16 at 14:23
  • @ddb it works when refreshed, but this isnt sufficient as the webapp will be used as an app on Apple Store using an app wrapper... I already get all the error codes above; i get the same error after the geolocation has been turned on if the webapp is open. I have very similar code to the code given above in the app. This doesn't solve my problem. – JasonBK Jul 04 '16 at 21:48
  • also the other SO answer given does not solve this problem either. – JasonBK Jul 04 '16 at 21:49