☞ Need help? I have an open spot for consulting in my schedule. Hire me.

Reasons why GPS failed

This code sample will allow you to show a message to the user regarding why you were unable to retrieve their GPS data.

var gpsWin = function (position) {
    console.log('GPS', position);
    alert("Current device GPS accuracy is " + parseInt(position.coords.accuracy) + "m.");
};
var gpsFail = function (e) {
    var msg = "Your device was unable to provide GPS coordinates";
    switch (e.code) {
    case 1:
        msg += " because you didn't allow it.";
        break;
    case 2:
        msg += " because your position is unavailable.";
        break;
    case 3:
        msg += " because it was taking too long.";
        break;

    }
    alert("GPS fail.\n\n"+msg);
};

Directions:
– add the success and error callback to your project.
– call navigator.geolocation.getCurrentPosition(gpsWin, gpsFail);

Note:
This snippet focuses on iOS development. Make sure to verify/adapt any vendor prefixes.

Back to main listing.