从Cordova插件上的函数返回值

时间:2015-03-19 22:06:24

标签: javascript android cordova gps

[编辑]

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

  1. 尝试使用地理定位器脚本(https://github.com/onury/geolocator)获取城市等;

  2. 如果有效,那很好。如果它返回的错误不是HTML5规范的一部分,请再次使用纯HTML5 Geolocation(diveintohtml5.info/geolocation.html);

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


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

    别介意'怎么做'的拼写错误。哪里有电话号码,它应该是 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()会按预期返回TRUE。

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

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

    ......我无法使其发挥作用。

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

    非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

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

尝试使用Geolocator脚本(https://github.com/onury/geolocator);

获取城市等

如果有效,那很好。如果它返回的错误不是HTML5规范的一部分,请再次使用纯HTML5 Geolocation(diveintohtml5.info/geolocation.html);

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

相关问题