Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-在短时间内获取网络GPS位置(最多10秒)_Android_Gps_Location_Criteria - Fatal编程技术网

Android-在短时间内获取网络GPS位置(最多10秒)

Android-在短时间内获取网络GPS位置(最多10秒),android,gps,location,criteria,Android,Gps,Location,Criteria,我正试图通过LocationManager设置一个快速而肮脏的GPS查找,它每半秒获取一个网络位置(500米以内),持续10秒。换句话说,我只是试图找到正确的粗略标准设置和正确的逻辑,以便在处理程序线程中没有更好的位置10秒后停止检查 我认为我的主循环应该是这样的: /** * Iteration step time. */ private static final int ITERATION_TIMEOUT_STEP = 500; //half-sec intervals public v

我正试图通过LocationManager设置一个快速而肮脏的GPS查找,它每半秒获取一个网络位置(500米以内),持续10秒。换句话说,我只是试图找到正确的粗略标准设置和正确的逻辑,以便在处理程序线程中没有更好的位置10秒后停止检查

我认为我的主循环应该是这样的:

/**
 * 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
       }
    }
}

我想知道的是,在我获取最后一个已知位置作为我的初始最佳位置并开始我的线程之后,我如何设置粗略的标准,以便获得比初始位置更准确的标准(以便比较两者的新鲜度),这通常与我当前的位置完全不同?

那么你要找的是询问设备获取粗略位置的最佳标准

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);  // Faster, no GPS fix.
String provider = locationManager.getBestProvider(criteria, true); // only retrieve enabled providers.
然后注册一个听众

locationManager.requestLocationUpdates(provider, ITERATION_TIMEOUT_STEP, MIN_LOCATION_UPDATE_DISTANCE, listener); //listener just implements android.location.LocationListener
在侦听器中,您将收到更新

 void onLocationChanged(Location location) {
     accuracy = location.getAccuracy(); //accuracy of the fix in meters
     timestamp = location.getTime(); //basically what you get from System.currentTimeMillis()
 }
在这一点上,我的建议是只根据准确度排序,因为你不能在10秒内改变你的位置那么多,但是粗略的位置更新在准确度上差别很大

我希望这有帮助