简体   繁体   中英

Return value from a function on Cordova plugin

[EDIT]

In the end, I gave up on this and just did some fallbacks like this:

  1. Try to get the city, etc with a Geolocator script ( https://github.com/onury/geolocator );

  2. if it worked, great. If it returns an error that's NOT part of the HTML5 spec, try again using pure HTML5 Geolocation (diveintohtml5.info/geolocation.html);

  3. If it worked, now I have at least the Latitude/Longitude coords. If it returns an error then most likely there's no GPS on the device.


I'm trying to use a Cordova plugin that checks if the GPS on the device is ON/OFF. The plugin in question is http://plugins.cordova.io/#/package/sk.tamex.locationandsettings .

Never mind the typos on the 'how-to'. Where there's telephonenumber it should be locationandsettings .

Anyways, I have a service to handle all the GPS stuff on my app, and it goes like this:

var isCordovaApp = !!window.cordova;

app.service('gpsSrvc', ['$interval', '$timeout', function($interval, $timeout) {

...some stuff...

    if (isCordovaApp) {
        var locationAndSettings = cordova.require("cordova/plugin/locationandsettings");
    }

    // Check for GPS on device
    self.isGpsEnabled = function(callback) {

        if (isCordovaApp) {

            locationAndSettings.isGpsEnabled(function(result) {

                if (result == true) {
                    console.log("GPS location ENABLED");
                    callback(true);
                } else {
                    console.log("GPS location DISABLED");
                    callback(false);
                }

            }, function() {
                console.log("Error checking for GPS on device");
                return false;
            });

        } else {
            return true ;
        }
    }

...some other stuff...

}]);

Now, when I run that on the browser, self.isGpsEnabled() returns TRUE, as expected.

When I try to run that on my phone, it displays various errors on the console, like this:

Error in Success callbackId: LocationAndSettings1329123004 : TypeError: undefined is not a function (cordova.js:305)

...and I can't make it work.

I also tried to use locationAndSettings.isGpsEnabled(function(result, callback){}) and the same happens. I just don't know how can I pass the result of locationAndSettings.isGpsEnabled() to self.isGpsEnabled() .

Any help would be much appreciated.

In the end, I gave up on this and just did some fallbacks like this:

Try to get the city, etc with a Geolocator script ( https://github.com/onury/geolocator );

if it worked, great. If it returns an error that's NOT part of the HTML5 spec, try again using pure HTML5 Geolocation (diveintohtml5.info/geolocation.html);

If it worked, now I have at least the Latitude/Longitude coords. If it returns an error then most likely there's no GPS on the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM