【问题标题】:Titanium/ Appcelerator/ Alloy: Unable to get current location despite success returning trueTitanium/Appcelerator/Alloy:尽管成功返回 true,但无法获取当前位置
【发布时间】:2017-03-28 22:26:37
【问题描述】:

我正在使用 iPhone 4s 模拟器运行以下代码:

Ti.Geolocation.getCurrentPosition(function(e){
    Ti.API.log(JSON.stringify(e));
});

上述输出的格式化版本提供以下信息:

{
    "code": 0,
    "type":"location",
    "error":"The operation couldn’t be completed. (kCLErrorDomain error 0.)",
    "source":{}, 
    "success":true
}

似乎有相当矛盾的信息。它说successtrue 但给出了错误消息。

此外,根据Location Results Docs,它说如果成功为真,它应该返回一个coords 字段。然而上面没有coords

还请注意以下两个返回 true 所以它不是权限问题:

Ti.Geolocation.locationServicesEnabled; Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS);

【问题讨论】:

    标签: ios titanium appcelerator appcelerator-titanium titanium-alloy


    【解决方案1】:

    有两点需要注意:

    1. locationServicesEnabled property for device

    2. hasLocationPermissions method

    首先,您需要检查第一个属性以确保设备位置已打开。

    然后您将检查 hasLocationPermissions,因为即使设备位置未打开,这也是正确的,我认为这是您的情况。

    示例代码:

    var authType = Ti.Geolocation. AUTHORIZATION_ALWAYS;
    
    if (Ti.Geolocation.locationServicesEnabled) {
        if (Ti.Geolocation.hasLocationPermissions(authType)) {
            // voila...
        } else {
            Ti.Geolocation.requestLocationPermissions(authType, function (e) {
                if (e.success) {
                    alert('voila...');
                }
            });
        }
    
    } else {
       alert('Please turn on your device's location');
    }
    

    【讨论】:

    • 我已经编辑了这个问题,它表明locationServicesEnabled 也是正确的。它也没有处理e.successtrue 的事实,但它给出了错误消息。这与文档上所说的相反。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多