javascriptandroidcordovageolocationintel-xdk

Intel XDK Geolocation won't find Lat/Long if location services off


I am using the Intel XDK to develop an app where i am specifically testing with android (Nexus 6P).

I have added the Cordova Geolocation plugin as well as Google Location Services for Cordova and have tested with my location services switched on and it correctly gets the position for both plugins.

However, when i switch off location services, nothing appears at all.

Here is the code i am currently using to get the location:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    var Geo={};
    if (navigator.geolocation) {
        console.log('Started location');
        console.log(cordova.plugins.locationServices.geolocation.getCurrentPosition(success, error));
        //navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy:true, maximumAge:Infinity, timeout:100000});
    }

    //Get the latitude and the longitude;
    function success(position) {
        console.log('SUCCESS location');
        Geo.lat = position.coords.latitude;
        Geo.lng = position.coords.longitude;
        populateHeader(Geo.lat, Geo.lng);
    }

    function error(){
        console.log("Geocoder failed");
    }

    function populateHeader(lat, lng){
        $('#Lat').attr('data-lat', lat);
        $('#Long').attr('data-long',lng);
    }
}

What i'm after, is either a way of asking the user to switch on location data, or even better would be to get the location of the user WITHOUT the location services being required.


Solution

  • You say "when I switch off location services" I assume you mean that you have gone into the Android settings and disabled the "Location History" or the turned off sharing "Location" on the device. In this case, you won't get any data because the user has elected not to share that information. See the warnings in the README for both plugins, this is considered very sensitive data and, therefore, Android has given users the ability to control sharing this information.

    I don't know if there is an API that would explicitly tell you whether or not that data has been disabled by the user. Regardless, you should probably report that you cannot get the data you need, which could be due to a variety of reasons:

    And then follow that up with some advice on what to change or fix in order to allow that data to be collected and used. Of course, if the user is not interested in ever sharing that information with your app you'll have to gracefully comply.


    EDIT 23 Feb 2017: See comment below for reference to some plugins that can help identify if location services are available.