0

I'm testing this simple geolocation code and it's not working on iOS devices for some reason. I've tested on MacOS, Windows, Windows Phone, Android phones, etc and it works as it should on all.

I always receive the "POSITION_UNAVAILABLE" error after granting permission.

navigator.geolocation.getCurrentPosition(
  function success(position) {
    var primary = getUniqueId();

    var url = endpointBaseurl +
      '?property_code=' + propertyCode +
      '&distribService=' + distribService +
      '&lat=' + position.coords.latitude +
      '&lon=' + position.coords.longitude +
      '&distribTarget=' + primary;

    //Update the line below to be one of your links.
    addMenuItem('Offers', url);
  },
  function error(err) {
    switch (err.code) {
      case 1:
        alert("You must allow location in order to receive tobacco offers. Please refresh and try again.")
        break;
      case 2:
        alert('POSITION_UNAVAILABLE');
        break;
      case 3:
        alert('Error 3');
        break;
      default:
        alert("Unable to determine your location. Please refresh and try again.");
    }
  }, options);

I've tested on physical devices as well as through Browserstack. The site is running on HTTPS.

Any ideas on why should isn't working or how I should go about debugging? I'm pulling my hair out over this one.

charlietfl
  • 170,828
  • 13
  • 121
  • 150
Aaron Silber
  • 463
  • 1
  • 5
  • 11
  • Have you looked at [this](http://stackoverflow.com/questions/30998246/html5-geolocation-not-working-all-the-time-on-ios) issue on StackOverflow? It might be of some help. – Christopher Bradshaw Apr 12 '17 at 16:25

1 Answers1

0

Edit: On this page, it says the following:

The acquisition of the geolocation failed because at least one internal source of position returned an internal error.

Try using PositionError.message (in your case, err.message):

Returns a human-readable DOMString describing the details of the error. Specifications note that this is primarily intended for debugging use and not to be shown directly in a user interface.


Make sure you have location services turned on and set to always or while using in the iOS settings.

Cameron
  • 1,049
  • 12
  • 24
  • Thanks, here's the err object; `PositionError {code: 3, message: "Timeout expired", PERMISSION_DENIED: 1, POSITION_UNAVAILABLE: 2, TIMEOUT: 3}` – Aaron Silber Apr 12 '17 at 17:26
  • I just increased the timeout to 20000, and receive this; `PositionError {code: 2, message: "The operation couldn’t be completed. (kCLErrorDomain error 0.)", PERMISSION_DENIED: 1, POSITION_UNAVAILABLE: 2, TIMEOUT: 3}` – Aaron Silber Apr 12 '17 at 17:31