2

Thanks in advance first.I build a website with an option to lead you to a specific place when you click button "Navigate". Everything works fine on desktop and everything works fine on mobile when GPS of the phone is enabled.

I use the google maps reference and it works just fine:

http://maps.google.com/maps?saddr=myLat,myLng&daddr=targetLat,targetLng

I do this on $('.button').click(). Not on $(window).load or $(document).ready. When you click the button, it calls the geolocation functions:

function getGeolocation(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
    } else {
        $('.error-message').html('<p>Your browser does not support this function</p>');
    }
}

function geoSuccess(position) {
    myLat = position.coords.latitude;
    myLng = position.coords.longitude;

    window.location.href = 'http://maps.google.com/maps?saddr=' + myLat + ',' + myLng + '&daddr=targetLat,targetLng';
}

function geoError(){
    switch(error.code) {
        alert('example alert - enable GPS');

        case error.PERMISSION_DENIED:
            $('.error-message').html('<p>User denied the request for Geolocation.</p>');
            break;
        case error.POSITION_UNAVAILABLE:
            $('.error-message').html('<p>Location information is unavailable.</p>');
            break;
        case error.TIMEOUT:
            $('.error-message').html('<p>The request to get user location timed out.</p>');
            break;
        case error.UNKNOWN_ERROR:
            $('.error-message').html('<p>An unknown error occurred.</p>');
            break;
    }
}

But the problem comes when the GPS is disabled.

First of all, sometimes, it doesn't enter in geoError(). I tried so many times, but the only thing I "found" is that it caches sometimes. Is that possible ?

But the main problem is that, when the GPS is disabled, the "example alert" pops up, then I turn my GPS on and click the button again, it doesn't work again. I have to refresh the page to apply the GPS turn on. I want to avoid that. Does anyone know why is that happening and is there a way to do what I want?

The other problem is when I load my page with GPS turned on. Then I turn it off and click the button. Nothing happens at all. It enter getGeolocation(), gets true and then it stops. It doesn't enter getCurrentPosition(). Any ideas?

3 Answers3

0

I had ever faced the same problem, and final I found the solution. You can change getCurrentPosition to watchPosition, it seems getCurrentPosition cannot recognize when location changes, watch help us do that. Try my solution, hope it helps you :) P/S you should settime out: 10000 for watchpostion or getcurrentpostion

dlam
  • 145
  • 2
  • 11
0

I've got the same problem on Android 6.0 and Chrome 59.0.3071.125.

In short: When I start a webapp with location Off, I receive a PositionError as expected.

However when I switch location back to On, every subsequent call to navigator.geolocation.getCurrentPosition or navigator.geolocation.watchPosition returns same error.

I've tried the same with maps.google.com and result is exactly the same (they show a snackbar Google Maps could not determine your precise location) so I believe that there is no workaround.

There is a similar post on ionic framework forum - They didn't find a solution either.

To sum up:

Looks like Chrome is checking for location services **on page load and doesn't update the state any more.

So only thing that helps is reloading the webapp which forces chrome to check again if location services are available.


Update:

this is a known bug in Chromium: 672301, 721977. Workaround is available in Chrome Mobile v59 behind lsd-permission-prompts flag.

piotr_cz
  • 8,755
  • 2
  • 30
  • 25
0

In your error function, the argument is missing.

function geoError(error){
    //code goes here
    }

Source: https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_error

shivamjj
  • 1
  • 1