【问题标题】:One shot getCurrentLocation cannot get a new location when mobile data is turned off in Titanium Android在 Titanium Android 中关闭移动数据时,一次拍摄 getCurrentLocation 无法获取新位置
【发布时间】:2013-05-30 04:54:35
【问题描述】:

我正在创建简单的应用程序以在按下按钮时获取当前设备的位置。这是我的代码:

var win = Titanium.UI.createWindow({  
    title:'Tab 1',
    backgroundColor:'#fff',
    layout:'vertical'
});

var btnGetLocation = Titanium.UI.createButton({
    title:'Get Location (Once)',
    width:'100dp'
});

btnGetLocation.addEventListener('click', function(e){
    getDeviceLocation();
});

function getDeviceLocation(){
    var longitude = 0.0;
    var latitude = 0.0;
    var altitude = 0;
    var heading = 0;
    var accuracy = 0;
    var speed = 0;
    var timestamp = '';
    var altitudeAccuracy = 0;

    var address = '';

    var errMessage = '';

    if (Titanium.Geolocation.locationServicesEnabled === false){
        errMessage = 'Geolocation access turned off';
    }
    else{
        Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
        Titanium.Geolocation.distanceFilter = 10;

        Titanium.Geolocation.getCurrentPosition(function(e){
            if (!e.success || e.error){
                errMessage = 'error: ' + JSON.stringify(e.error);
                return;
            }

            if (typeof e.coords.longitude !== 'undefined' && e.coords.longitude !== null){longitude = e.coords.longitude;}
            if (typeof e.coords.latitude !== 'undefined' && e.coords.latitude !== null){latitude = e.coords.latitude;}
            if (typeof e.coords.altitude !== 'undefined' && e.coords.altitude !== null){altitude = e.coords.altitude;}
            if (typeof e.coords.heading !== 'undefined' && e.coords.heading !== null){heading = e.coords.heading;}
            if (typeof e.coords.accuracy !== 'undefined' && e.coords.accuracy !== null){accuracy = e.coords.accuracy;}
            if (typeof e.coords.speed !== 'undefined' && e.coords.speed !== null){speed = e.coords.speed;}
            if (typeof e.coords.timestamp !== 'undefined' && e.coords.timestamp !== null){timestamp = e.coords.timestamp;}
            if (typeof e.coords.altitudeAccuracy !== 'undefined' && e.coords.altitudeAccuracy !== null){altitudeAccuracy = e.coords.altitudeAccuracy;}
        });

        // reverse geocoding
        Titanium.Geolocation.reverseGeocoder(latitude,longitude,function(e){
            if (e.success) {
                var places = e.places;
                if (places && places.length) {
                    address = places[0].address;
                } else {
                    address = "No address found";
                }
            }
            else {
                address = e.error;
            }

            alert('Longitude = ' + longitude + '\nLatitude = ' + latitude + '\nAltitude = ' + altitude + '\nHeading = ' + heading + '\nAccuracy = ' + accuracy + '\nSpeed = ' + speed + '\nTimestamp = ' + new Date(timestamp) + '\nAltitude Accuracy = ' + altitudeAccuracy + '\nAddress = ' + address + '\nError Message = ' + errMessage);
        });
    }
}

win.add(btnGetLocation);

win.open();

条件是:设备没有任何互联网连接(数据连接)并且仅打开了 GPS。当我按下按钮(一次获取位置)时,我无法获得新位置..设备仍然显示我的旧位置和旧时间戳,即使我当前的位置改变了也永远不会获得新位置..有人吗知道如何仅通过 GPS 连接获取当前位置(一次拍摄)吗?非常感谢..

【问题讨论】:

  • 可能是您的代码是正确的。请长途跋涉后试一试。例如相差2到3公里。那么你可以得到准确的结果。
  • @MRT 嗨,我尝试从我的第一个位置移动到其他位置..但它仍然显示我的旧位置..你有其他建议吗?谢谢..

标签: android geolocation gps titanium titanium-mobile


【解决方案1】:

如果您在 Android 下使用 getCurrentPosition,这不是 Titanium 错误。 如果您阅读文档:

*从设备中检索最后一个已知位置。

在 Android 上,没有打开无线电来更新位置,而是使用缓存的位置。

在 iOS 上,如果位置太“旧”,则可能会使用无线电。*

这意味着 getCurrentPosition(在 Android 下)返回系统缓存的最后一个位置,直到应用程序(使用位置服务)需要一个新位置(如谷歌地图)。 所以......你可以做的是在应用程序启动时不要使用getCurrentPosition......而是使用一个名为“location”的事件,每次有新的位置可用时触发。

卢卡

【讨论】:

  • icic.. 这对我很有帮助,因为我不知道.. 谢谢老兄.. 你太棒了.. :)
  • 如果您使用的是 Android 系统,那么您可以做的最好的事情就是妥善管理地理定位事件。这就是我所做的,因为没有办法强制刷新当前位置。
  • 你必须创建一个函数来管理这个事件 Titanium.Geolocation.addEventListener('location', yourFunction);当操作系统刷新当前位置时触发。 (对不起,双重评论)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 2013-09-30
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
相关资源
最近更新 更多