反应本机ios:默认情况下,geoloc的准确性较差 [英] react native ios: geoloc has poor accuracy by default

查看:331
本文介绍了反应本机ios:默认情况下,geoloc的准确性较差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用navigator.geolocation.watchPosition和getCurrentPosition实施健身跟踪器。它在Android和ios模拟器上工作正常,具有5/10米的准确性,但在iPhone 5s上,我有一个可怕的准确度(50/65)。



我发现当我在ios上同时运行一个现有的健身追踪器(strava)时,突然我的应用程序以非常好的精度检索了GPS坐标。



缺少一些配置,因为默认情况下,我的应用在iOS上不使用高精度。
任何想法如何解决这个问题?



代码:

  const GPS_TIMEOUT_GET = 5000; 
const GPS_MAX_AGE = 1000;

const GPS_DISTANCE_FILTER = 5;
const GPS_TIMEOUT_WATCH = 60000;
const GPS_MIN_ACCURACY = 50;

//开始全时间观看GPS。排出电池但带来最佳准确度(我们需要的)
let watchId = navigator.geolocation.watchPosition((position)=> {
}
,(error)=> {
console.log(错误);
}
,{
enableHighAccuracy:true,
timeout:GPS_TIMEOUT_WATCH,
maximumAge:GPS_MAX_AGE,
distanceFilter :GPS_DISTANCE_FILTER
});

//每隔X毫秒检查GPS)
let intervalId = BackgroundTimer.setInterval(()=> {
navigator.geolocation.getCurrentPosition((geoPosition)=> {
if(geoPosition.coords.accuracy <= GPS_MIN_ACCURACY){
let position = createPositionObjectFromGeoPosition(geoPosition);
dispatch({type:GPS_UPDATE_LOC,payload:position})
}
}
,(error)=> {
console.log(error);
}
,{
enableHighAccuracy:true,
超时:GPS_TIMEOUT_GET,
maximumAge:GPS_MAX_AGE
});
},时间);

Info.plist:

 <密钥GT; NSLocationAlwaysUsageDescription< /密钥GT; 
< string>您的位置用于在乘车过程中跟踪您的位置并实时获取反馈信息(距离骑行,持续时间,速度等)。< / string>
< key> UIBackgroundModes< / key>
< array>
< string>位置< / string>
< / array>
< key> UIRequiredDeviceCapabilities< / key>
< array>
< string> armv7< / string>
< string> location-services< / string>
< string> gps< / string>
< / array>


解决方案

我用 https://github.com/timfpark/react-native-location ,它使用了iOS本地位置api,并更新了我的代码在Android上,它保持不变,并使用已经工作的w3c地理位置api

$ 在iOS上,我开始跟踪上面的iOS专用api,而不是使用w3c geoloc函数'watchPosition'。要定期检索位置,来自w3c geoloc的getPosition仍然有效(正如我的应用程序同时运行strava时所观察到的那样),所以我保留它(可能会重构它以便在iOS上完全删除w3c api,如果它不令人满意)。


更新:在许多用户反馈之后,我对Android上的JS API watchPosition的准确性不满意。我的应用程序是一个健身追踪器,所以我需要非常高的准确性,但它可能会因您的需要而有所不同。我建议仔细测试一下,如果不满意,请改用本机android API。


I'm implementing a fitness tracker with navigator.geolocation.watchPosition and getCurrentPosition . It works fine on android and on ios emulator, have 5/10m accuracy, but on iphone 5s, I have a terrible accuracy (50/65).

I found out that when I was running an existing fitness tracker at the same time (strava) on ios, suddenly my app was retrieving GPS coordinates with very good accuracy.

So clearly I must be missing some configuration because by default my app does not use high accuracy on iOS. Any idea how to solve that problem?

Code:

const GPS_TIMEOUT_GET = 5000;
    const GPS_MAX_AGE = 1000;

    const GPS_DISTANCE_FILTER = 5;
    const GPS_TIMEOUT_WATCH = 60000;
    const GPS_MIN_ACCURACY = 50;

    //start the GPS into full time watching. Drains battery but brings best accuracy (required for our needs)
    let watchId = navigator.geolocation.watchPosition((position) => {
        }
        , (error) => {
            console.log(error);
        }
        , {
            enableHighAccuracy: true,
            timeout: GPS_TIMEOUT_WATCH,
            maximumAge: GPS_MAX_AGE,
            distanceFilter: GPS_DISTANCE_FILTER
        });

    //check GPS every X milliseconds)
    let intervalId = BackgroundTimer.setInterval(() => {
        navigator.geolocation.getCurrentPosition((geoPosition) => {
                if (geoPosition.coords.accuracy <= GPS_MIN_ACCURACY) {
                    let position = createPositionObjectFromGeoPosition(geoPosition);
                    dispatch({type: GPS_UPDATE_LOC, payload: position})
                }
            }
            , (error) => {
                console.log(error);
            }
            , {
                enableHighAccuracy: true,
                timeout: GPS_TIMEOUT_GET,
                maximumAge: GPS_MAX_AGE
            });
    }, time);

Info.plist:

<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is used to track your position during a ride and get feedback (distance ridden, duration, speed, etc) in real time and afterwards.</string>
<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
    <string>location-services</string>
    <string>gps</string>
</array>

解决方案

I used https://github.com/timfpark/react-native-location which uses iOS native location api, and updated my code so that

  • on android, it stays unchanged and uses the w3c geolocation api which already works

  • on iOS, I start tracking with the above iOS dedicated api, instead of using w3c geoloc function 'watchPosition'. To retrieve location at regular intervals, getPosition from w3c geoloc still works (as observed when running strava at the same time as my app), so I kept it (might refactor it to remove completely w3c api on iOS later if it's not satisfying).

UPDATE: after many user feedbacks, I'm not satisfied with the accuracy of the JS API watchPosition on Android. My application is a fitness tracker so I need very high accuracy, but it might be different for your needs. I advise to test it carefully, and if unsatisfying use a native android API instead.

这篇关于反应本机ios:默认情况下,geoloc的准确性较差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆