Return 来自 Cordova 插件函数的值

Return value from a function on Cordova plugin

[编辑]

最后,我放弃了这个,只是做了一些这样的后备:

  1. 尝试使用 Geolocator 脚本获取城市等 (https://github.com/onury/geolocator);

  2. 如果有效,那就太好了。如果它 returns 一个不属于 HTML5 规范的错误,请使用纯 HTML5 地理定位(diveintohtml5.info/geolocation.html);

  3. 如果有效,现在我至少有 Latitude/Longitude 坐标。如果它 returns 是一个错误,那么很可能设备上没有 GPS。


我正在尝试使用 Cordova 插件检查设备上的 GPS 是否 ON/OFF。有问题的插件是 http://plugins.cordova.io/#/package/sk.tamex.locationandsettings.

别在意 'how-to' 上的拼写错误。 telephonenumber 应该是 locationandsettings.

无论如何,我有一项服务可以处理我应用程序上的所有 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...

}]);

现在,当我在浏览器上 运行 时,self.isGpsEnabled() returns TRUE,正如预期的那样。

当我在 phone 上尝试 运行 时,它会在控制台上显示各种错误,如下所示:

成功时出错 callbackId: LocationAndSettings1329123004 : TypeError: undefined is not a function (cordova.js:305)

...我无法让它工作。

我也尝试使用 locationAndSettings.isGpsEnabled(function(result, callback){}) 并且发生了同样的情况。我只是不知道如何将 locationAndSettings.isGpsEnabled() 的结果传递给 self.isGpsEnabled().

如有任何帮助,我们将不胜感激。

最后,我放弃了这个,只是做了一些这样的后备:

尝试使用 Geolocator 脚本获取城市等信息 (https://github.com/onury/geolocator);

如果有效,那就太好了。如果它 returns 一个不属于 HTML5 规范的错误,请使用纯 HTML5 地理定位(diveintohtml5.info/geolocation.html);

如果有效,现在我至少有 Latitude/Longitude 个坐标。如果它 returns 是一个错误,那么很可能设备上没有 GPS。