如何强制实现PhoneGap GPS地理定位的最低精度? [英] How do I force minimum accuracy for PhoneGap GPS Geolocation?

查看:139
本文介绍了如何强制实现PhoneGap GPS地理定位的最低精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,要求用户有一些准确的GPS位置。现在我正在使用:

$ p $ navigator.geolocation.getCurrentPosition(geolocationSuccess,geolocationError,
{maximumAge:60000, timeout:8000,enableHighAccuracy:true});

运行应用程序时,我得到 position.coords.accuracy 值有时或更高, 29000 。这是不可接受的。我怎么能强制更低(更准确)的GPS位置?



它发生在iOS和Android上。



注意我使用

 < gap:plugin name =cordova-plugin-geolocationsource =npm/ > 


解决方案

最佳准确度可以达到:




  • 设定较低的 maximumAge
  • timeout 取决于你的应用程序的使用方式(它是主要用于汽车的navi-app还是主要用于徒步旅行的jogger-app)。基本规则:设置较高的超时时,您授予硬件GPS芯片更多时间请求准确的坐标。

  • 当然 enableHighAccuracy 也必须设置为 true





让我更详细地解释以上几点。 / p>

当您将 maximumAge 设置为 0 (零)时永远不要让live-gps-坐标(不缓存)回来,因为即使是最准确的坐标也有一定的年龄。这真的取决于你的应用程序需求,它使我到 timeout



我会将超时设置为5秒,因为我使用的大多数现代设备都可以在0.5-3秒之间获得坐标。它取决于你的应用移动的速度有多快(汽车,徒步)。移动应用程序越快,必须设置超时>,否则应用程序可能会获得太不准确的坐标。另一方面, timeout 一定不能设置为 0 ,因为这个请求现在每次都会被取消永远不要让live-gps-坐标回来。






你也可以在github上制作自己的插件或工程例如以更多的控制:


  • 水平/垂直精度

  • 以确保总是使用gps-hardware-chip(总是初始化Android的 LocationManager.GPS_PROVIDER )。因为即使你设置 enableHighAccuracy true ,也不能保证gps-hardware-chip真的被使用,它是只是你告诉这个插件使用(如果可能的话)gps卫星的愿望,而且很大程度上取决于环境条件,这使我可以进入下一段。

  • ....
  • 请注意:由于恶劣的环境条件(街道峡谷,强烈的云雾等),要求最准确的坐标不一定总是可能的。是否要求您的应用必须随时随地获取坐标?然后你必须放松你的准确性要求。



    最后但并非最不重要的一点,在一个成功的请求后,你总是可以检查坐标的准确性 - 属性不能超过一定的阈值(取决于应用程序的要求),如果它确实发出另一个请求,例如: var threshold =APP-ACCURANCY-REQUIREMENT-NUMBER-HERE;

    函数requestLocation(回调函数){
    navigator.geolocation.getCurrentPosition(
    函数(位置){
    console.log(position);
    // (position.coords.accuracy< threshold)callback(position);
    else requestLocation(callback);
    else requestLocation(callback);这个位置是否满足您的应用程序的ACCURANCY-REQUIREMENT?如果因此停止请求另外请求

    },
    函数(err){
    console.log(err);
    },
    {
    maximumAge:60000,// IS A 1 -MINUTE-COORDINATE TOO OLD CARIVE驾驶130KM /小时 - > 36M /秒
    超时:8000,//是驾驶汽车时8秒钟等待的时间太长
    enableHighAccuracy:true // YES,这个属性必须设置为TRUE
    }
    );

    $ b $ requestLocation(function(position){
    //精度低于ACCURANCY-REQUIREMENT的位置
    console.log('请求精确位置' ,position);
    });

    希望这个故事能帮助你,祝你好运!


    I am building an app that requires somewhat accurate GPS position of the user. Right now I'm using:

    navigator.geolocation.getCurrentPosition(geolocationSuccess,geolocationError, 
        { maximumAge: 60000, timeout:8000 , enableHighAccuracy: true } );
    

    When running the app however, I'm getting position.coords.accuracy with a value of 29000 sometimes or higher. This is not acceptable. How can I force a lower (more accurate) GPS position?

    It's happening on both iOS and Android.

    Note I'm using

    <gap:plugin name="cordova-plugin-geolocation" source="npm" />
    

    解决方案

    The best accurancy can be achieved:

    • The lower maximumAge is set.
    • timeout depends on how your app is used (is it a "navi-app" mostly used in cars or is it "jogger-app" mostly used on foot?). Basic rule: the higher timeout is set the more time you grant your hardware-gps-chip to request accurate coordinates.
    • Of course enableHighAccuracy must be set true as well.

    Let me explain the points above in (a little bit) more detail.

    When you set maximumAge to 0 (Zero) you never get "live"-gps-coordinates (not cached) back, because even most accurate coordinates have got a certain age. It really depends on your app-requirements which brings me to timeout.

    I would set timeout to 5 secs because most modern devices I have used can get coordinates between 0.5-3 secs. It depens on how quick your app is moved (car, on foot). The quicker the app is moved the less timeout has to be set otherwise the app can get too inaccurate coordinates. On the other hand timeout must not be set to 0, because the request would be canceled right now every time and you never get "live"-gps-coordinates back.


    You can also make your own plugin or fork a project on github for instance to have more control over:

    • horizontal/vertical accuracies
    • to make sure that the gps-hardware-chip is always used (initialize always Android's LocationManager.GPS_PROVIDER). Because even when you set enableHighAccuracy to true there is no guarantee that the gps-hardware-chip is really used, it's only a "wish" that you tell this plugin to use (if possible) gps-satellites and strongly depends on environmental conditions which brings me to the next paragraph.
    • ....

    Note that: It is not always possible to request most accurate coordiantes because of bad environmental conditions (street canyon, strong cloudiness etc.). Is is a requirement that your app has to get coordinates everywhere and everytime? Then you have to loosen your accuracy-requirement for sure.

    And last but not least, after a successful request you can always check for the coordinate‘s accuracy-attribute that must not exceed a certain threshold (depending on app-requirement) and if it does then make another request like in this example:

    var threshold = "APP-ACCURANCY-REQUIREMENT-NUMBER-HERE";
    
    function requestLocation(callback) {
      navigator.geolocation.getCurrentPosition(
        function (position) {
          console.log(position);
          // Does this position fulfill the ACCURANCY-REQUIREMENT of your app? IF SO THEN STOP REQUESTING OTHERWISE REQUEST AGAIN
          if (position.coords.accuracy < threshold) callback(position);
          else requestLocation(callback);
        }, 
        function (err) {
          console.log(err);
        }, 
        {
          maximumAge: 60000,// IS A 1-MINUTE-COORDINATE TOO OLD WHILE DRIVING BY CAR? 130KM/hour -> 36 M/sec
          timeout: 8000 ,// IS A WAITING-TIME OF 8 SECONDS TOO LONG WHILE DRIVING BY CAR? 
          enableHighAccuracy: true // YES, THIS ATTRIBUTE MUST BE SET TO TRUE
        }
      );
    }
    
    requestLocation(function (position) {
      // position that has got an accuracy less than ACCURANCY-REQUIREMENT
      console.log('request accurate position', position);
    });
    

    Hope this "story" helps you and Good luck!

    这篇关于如何强制实现PhoneGap GPS地理定位的最低精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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