从Cordova插件上的函数返回值

时间:2020-12-01 20:28:48

[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);

    尝试使用Geolocator脚本获取城市等(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);

    如果它奏效,那很好。如果它返回的错误不属于HTML5规范,请再次使用纯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.

    如果它有效,现在我至少有纬度/经度坐标。如果它返回错误,则很可能设备上没有GPS。


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.

我正在尝试使用Cordova插件来检查设备上的GPS是否为ON / OFF。有问题的插件是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:

无论如何,我有一个服务来处理我的应用程序上的所有GPS的东西,它是这样的:

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.

现在,当我在浏览器上运行它时,self.isGpsEnabled()会按预期返回TRUE。

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().

我也尝试使用locationAndSettings.isGpsEnabled(函数(结果,回调){})并发生同样的事情。我只是不知道如何将locationAndSettings.isGpsEnabled()的结果传递给self.isGpsEnabled()。

Any help would be much appreciated.

任何帮助将非常感激。

1 个解决方案

#1


0  

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);

尝试使用Geolocator脚本获取城市等(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);

如果它奏效,那很好。如果它返回的错误不属于HTML5规范,请再次使用纯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.

如果它有效,现在我至少有纬度/经度坐标。如果它返回错误,则很可能设备上没有GPS。

#1


0  

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);

尝试使用Geolocator脚本获取城市等(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);

如果它奏效,那很好。如果它返回的错误不属于HTML5规范,请再次使用纯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.

如果它有效,现在我至少有纬度/经度坐标。如果它返回错误,则很可能设备上没有GPS。