Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Fix for “GPS Provider Disabled” using Apache Cordova on Android

DZone's Guide to

Fix for “GPS Provider Disabled” using Apache Cordova on Android

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

The Problem

I recently ran into the issue of “GPS Provider Disabled” when using Apache Cordova on Android. After reading many threads, the fix seemed to always be to set the permissions in config.xml with the following code:

<gap:plugin name="org.apache.cordova.geolocation" />
<feature name="http://api.phonegap.com/1.0/geolocation"/>

After doing that, you would need to check Settings –> Location and make sure it is on.

If it didn’t work after that, well you were pretty much on your own…

The Resolution

After digging I found that if I include enableHighAccuracy to false, then it would use Assisted GPS rather than satellite positioning. Sure, the results may not be as high as you would like but it is better than an ugly dialog box saying “GPS Provider Disabled”.

Here is a full sample:

<!DOCTYPE html>
<html>
  <head>
    <title>Device Properties Example</title>
 
    <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
    <script type="text/javascript" charset="utf-8">
 
    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);
 
    var watchID = null;
 
    // Cordova is ready
    //
    function onDeviceReady() {
        // Changed this from true to false to prevent dialog popup on Android devices.
        var options = { enableHighAccuracy: false };
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
    }
 
    // onSuccess Geolocation
    //
    function onSuccess(position) {
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                            'Longitude: ' + position.coords.longitude     + '<br />' +
                            '<hr />'      + element.innerHTML;
    }
 
    // clear the watch that was started earlier
    // 
    function clearWatch() {
        if (watchID != null) {
            navigator.geolocation.clearWatch(watchID);
            watchID = null;
        }
    }
 
    // onError Callback receives a PositionError object
    //
    function onError(error) {
      alert('code: '    + error.code    + '\n' +
            'message: ' + error.message + '\n');
    }
 
    </script>
  </head>
  <body>
    <p id="geolocation">Watching geolocation...</p>
    <button onclick="clearWatch();">Clear Watch</button>     
  </body>
</html>

Wrap-Up

I hope that helps anyone out there that is having problems with this!

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Michael Crump. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}