Android位置间隔更改

时间:2015-03-10 14:02:41

标签: java android gps location

我试图通过根据用户的当前速度更改位置更新的最小间隔来使我的位置更加智能化。以下代码存在问题,没有速度,我每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");
                }

            }
        }

2 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分:0)

我认为问题在于,每次调用onLocationChanged时都会调用removeUpdates和requestLocationUpdates。当您拨打requestLocationUpdates时,GPS会立即开始工作并尝试尽快修复您的位置。

当位置修复后,下次修复将在您指定的延迟后执行,但您不允许这样做,因为您每次都通过调用removeUpdates和requestLocationUpdates重新启动该过程。我认为这就是为什么你几秒钟后总能得到GPS修复。