安卓定位间隔更改

3

我试图通过改变用户当前速度来使我的位置算法更加智能化,从而改变位置更新的最小间隔。但以下代码存在问题,无论速度如何,我都会不断地在每8秒内更新。希望得到任何帮助。

以下是主要代码部分:
 public void onLocationChanged(Location location) {

        Location lastLocation = getLastLocation();

        if(lastLocation != null){

            double speed=location.distanceTo(lastLocation)/((getMilSecFromDate(getCurrentDateTime())-lastLocation.getTime())/1000);
            speedTmp = speed;

            if(!isLocEqual(location,lastLocation)){
                Toast.makeText(LocationService.this, speed+"   "+"Location:  " + "Time:  " + getCurrentDateTime() + "   Latitude:  " + location.getLatitude() + "  Longitude:  "
                        + location.getLongitude(), Toast.LENGTH_SHORT).show();

                timeMoved=Calendar.getInstance().getTimeInMillis();
                //if battery is under 20%, service is shutted down
                if(batPercent<20){
                    onDestroy();
                    Toast.makeText(LocationService.this, "Battery under 20%, charge", Toast.LENGTH_SHORT).show();
                }

                if(speed<=10&&speed>2){
                    Toast.makeText(LocationService.this, "Hodanje - speed " +speed, Toast.LENGTH_LONG).show();
                    MIN_TIME=1000*60;
                    locationManager.removeUpdates(locationListener);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
                    Log.i("gpsSetvice", "speed<10");
                }
                else if(speed>10 && speed<60){
                    Toast.makeText(LocationService.this, "Izmedju 10 i 60 - speed " +speed, Toast.LENGTH_LONG).show();
                    MIN_TIME=1000*30;
                    locationManager.removeUpdates(locationListener);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
                    Log.i("gpsSetvice", "speed>10 && speed<60");
                }
                else if(speed>=60 && speed<100){
                    Toast.makeText(LocationService.this, "izmedju 60 i 100 " +speed, Toast.LENGTH_LONG).show();
                    MIN_TIME=1000*20;
                    locationManager.removeUpdates(locationListener);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
                    Log.i("gpsSetvice", "speed>=60 && speed<100");
                }
                else if(speed>=100){
                    Toast.makeText(LocationService.this, "Preko 100 - speed " +speed, Toast.LENGTH_LONG).show();
                    MIN_TIME=1000*2*60;
                    locationManager.removeUpdates(locationListener);
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, 0, locationListener);
                    Log.i("gpsSetvice", "speed>=MILE");
                }

            }
        }

您必须启动并停止位置管理器,以使更改生效。 - danny117
你是怎么想停止位置管理器的?我已经使用了locationManager.removeUpdates();。 - user1791310
我的错,没看到那个。 - danny117
我看不到你的代码中有8000毫秒,要得到8秒。 - danny117
2个回答

1
我认为问题在于,每次调用onLocationChanged时,您都调用removeUpdates和requestLocationUpdates。当您调用requestLocationUpdates时,GPS会立即开始工作,并尝试尽快修复您的位置。
当位置被确定后,下一个修复将在您指定的延迟之后执行,但是您不允许这样做,因为每次都调用removeUpdates和requestLocationUpdates重新启动该过程。我认为这就是为什么您总是在几秒钟后获得GPS修复的原因。

谢谢回复。我已经删除了removeUpdates()语句,但仍然得到与之前相同的结果。 - user1791310
我认为问题在于requestLocationUpdates,因为它在调用时立即启动GPS定位修复,如果我没错的话... :-) - Berťák

0

从您的GPS位置获取速度如下:

int updateCount=0;
float totalSpeedSum=0.0f;
float avgSpeed=0.0f;
public void onLocationChanged(Location location) {

    Location lastLocation = getLastLocation();

    // speed in m/sec
    float curSpeed=location.getSpeed();
    updateCount++;
    totalSpeedSum=totalSpeedSum+curSpeed;
    avgSpeed=totalSpeedSum/updateCount;

}

谢谢回复,我知道那个函数,但我需要平均速度。 - user1791310
在每次位置更新时添加您的速度,并计算每次位置更新,然后使用公式平均速度=(总速度和)/计数。请参见编辑后的答案。 - Ravi Shanker Yadav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接