Android的 - 获得一个短的时间内网络的GPS定位(最长10秒) [英] Android - Getting Network GPS location within a short time frame (10 seconds max)

查看:357
本文介绍了Android的 - 获得一个短的时间内网络的GPS定位(最长10秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立通过的LocationManager一个快速和肮脏的GPS查找它取一个网络位置(500米以内),每半,共10秒钟。换句话说,我只是想找到正确的粗标准设置和正确的逻辑来阻止我的处理程序线程内没有更好的位置10秒后检查。

I'm trying to set up a quick and dirty GPS lookup via LocationManager which fetches a network location (within 500 meters) every half second for ten seconds. In other words, I'm just trying to find the correct coarse Criteria settings and the correct logic to stop checking after 10 seconds of no better locations within my Handler thread.

我想我主要的循环应该是这个样子:

I'm thinking my main loop should look something like this:

/**
 * Iteration step time.
 */
private static final int ITERATION_TIMEOUT_STEP = 500; //half-sec intervals
public void run(){
    boolean stop = false;
    counts++;
    if(DEBUG){
        Log.d(TAG, "counts=" + counts);
    }

    //if timeout (10 secs) exceeded, stop tying
    if(counts > 20){ 
        stop = true;
    }

    //location from my listener
    if(bestLocation != null){
       //remove all network and handler callbacks
    } else {
       if(!stop){
          handler.postDelayed(this, ITERATION_TIMEOUT_STEP);
       } else {
          //remove callbacks
       }
    }
}

我想知道的是,在我取最后已知的位置我最初的最佳位置,并拉开我的线程,我该如何设置粗准则,使我得到一个准确比最初的一家(以比较两个新鲜度),这通常是我的当前位置截然不同?

What I want to know is after I fetch the last known location as my initial best location and kick off my thread, how do I set up the coarse criteria so that I receive a more accurate one than the initial one (in order to compare the two for freshness), which is usually diametrically different from my current location?

推荐答案

那么你正在寻找的是要求设备最好的标准来获取粗略的位置。

Well what you are looking for is to ask the device for the best criteria to fetch a coarse location.

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);  // Faster, no GPS fix.
String provider = locationManager.getBestProvider(criteria, true); // only retrieve enabled providers.

然后,只需注册一个监听器

Then just register a listener

locationManager.requestLocationUpdates(provider, ITERATION_TIMEOUT_STEP, MIN_LOCATION_UPDATE_DISTANCE, listener); //listener just implements android.location.LocationListener

在听者收到更新这样

in the listener you receive an update as such

 void onLocationChanged(Location location) {
     accuracy = location.getAccuracy(); //accuracy of the fix in meters
     timestamp = location.getTime(); //basically what you get from System.currentTimeMillis()
 }

在这一点上我的建议是那种完全基于准确,因为你不能改变你的位置,那么多在10秒内又粗​​的位置更新精确度差异很大。

At this point my advice is to sort solely based on accuracy as you can't vary your location that much in 10 seconds yet coarse location updates vary greatly in accuracy.

我希望这有助于。

这篇关于Android的 - 获得一个短的时间内网络的GPS定位(最长10秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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