【问题标题】:minimum update time in phoneGap App with watchPosition or getCurrentPosition?使用 watchPosition 或 getCurrentPosition 在 phoneGap App 中的最短更新时间?
【发布时间】:2013-09-30 16:41:20
【问题描述】:

我在快速更新 GPS 位置时遇到了一些问题,我需要每 100 毫秒更新一次位置。在我的解决方案中,我观察到 GPS 位置会每秒更新一次,而不是更快:( 我试过 setInterval:

function localize(){


    if(navigator.geolocation)
    {


        navigator.geolocation.getCurrentPosition(function(position){

                                                 var element = document.getElementById('geolocation');
                                                 element.innerHTML = 'Latitude: '          + position.coords.latitude         + '<br />' +
                                                 'Longitude: '         + position.coords.longitude        + '<br />' +
                                                 'Altitude: '          + position.coords.altitude         + '<br />' +
                                                 'Accuracy: '          + position.coords.accuracy         + '<br />' +
                                                 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
                                                 'Heading: '           + position.coords.heading          + '<br />' +
                                                 'Speed: '             + position.coords.speed            + '<br />' +
                                                 'Timestamp: '         + position.timestamp               + '<br />';
                                                 },function(error){
                                                 alert('code: '    + error.code    + '\n' +
                                                       'message: ' + error.message + '\n');
                                                 }, { maximumAge:100, timeout:100, enableHighAccuracy:true  });

    }else{
        handleNoGeolocation(false);
    }

}
localize();
setInterval(localize, 100);
}

设置超时:

localize();
function localize(){


    if(navigator.geolocation)
    {


        navigator.geolocation.getCurrentPosition(function(position){

                                                 var element = document.getElementById('geolocation');
                                                 element.innerHTML = 'Latitude: '          + position.coords.latitude         + '<br />' +
                                                 'Longitude: '         + position.coords.longitude        + '<br />' +
                                                 'Altitude: '          + position.coords.altitude         + '<br />' +
                                                 'Accuracy: '          + position.coords.accuracy         + '<br />' +
                                                 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
                                                 'Heading: '           + position.coords.heading          + '<br />' +
                                                 'Speed: '             + position.coords.speed            + '<br />' +
                                                 'Timestamp: '         + position.timestamp               + '<br />';
                                                 },function(error){
                                                 alert('code: '    + error.code    + '\n' +
                                                       'message: ' + error.message + '\n');
                                                 }, { maximumAge:100, timeout:100, enableHighAccuracy:true  });
        setTimeout(localize, 100);
    }else{
        handleNoGeolocation(false);
    }

}

setInterval(localize, 100);
}

或whatchPosition:

localize();
function localize(){


    if(navigator.geolocation)
    {


        navigator.geolocation.watchPosition(function(position){

                                                 var element = document.getElementById('geolocation');
                                                 element.innerHTML = 'Latitude: '          + position.coords.latitude         + '<br />' +
                                                 'Longitude: '         + position.coords.longitude        + '<br />' +
                                                 'Altitude: '          + position.coords.altitude         + '<br />' +
                                                 'Accuracy: '          + position.coords.accuracy         + '<br />' +
                                                 'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
                                                 'Heading: '           + position.coords.heading          + '<br />' +
                                                 'Speed: '             + position.coords.speed            + '<br />' +
                                                 'Timestamp: '         + position.timestamp               + '<br />';
                                                 },function(error){
                                                 alert('code: '    + error.code    + '\n' +
                                                       'message: ' + error.message + '\n');
                                                 }, { maximumAge:100, timeout:100, enableHighAccuracy:true  });
        setTimeout(localize, 100); //with or without
    }else{
        handleNoGeolocation(false);
    }

}


}

我尝试过使用或不使用 maximumAge 和 timeout 选项。 我的设备是 iPhone 5。

【问题讨论】:

    标签: iphone cordova geolocation gps


    【解决方案1】:

    消费类手机中的 GPS 设备每秒更新不超过一次。 不管你做什么,你都不会得到 100 毫秒的间隔。

    因此更改您的算法(插值)以使用 1 秒。

    【讨论】:

      【解决方案2】:

      是的,GPS 无法在 1 秒内发送更新。 我在 Cordova 制作的 Android 项目中找到了这一点。我认为这可以帮助你。也许在IOS项目中也是一样的?

      我刚刚在 Cordova/PhoneGap 中发现了一个大问题: - 当您打开由 cordova 构建的 Android 项目时(在命令行中),requestUpdate 设置为..... 60 秒 !!!!!!!!! !!您每分钟都有一个新位置!即使您使用 GPS (enableHighAccuracy="true")。因此,尝试将其设置为 1000,您可以在 org.apache.cordova.geolocation 包中找到 LocationManager.GPS_PROVIDER -> GPSListener 类!

      可可

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-29
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多