Giacomo Balli profile picture
Giacomo Balli
Your Technology Advisor

Over two decades of experience at your service.
I help small business owners make better decisions.

Let's Chat LinkedIn

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.

"+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.

Published: Sun, Jan 10 2016 @ 18:51:54
Back to Blog