我在使用传统onLocationChanged
调用获取位置更新的正确时间时遇到了一些问题。我已经设置了GPS
参数,requestLocationUpdates
并且我的getLatitude();
getLongitude();
和getAltitude();
值在适当的时间间隔内更新得很好,但是我的时间值似乎落后于一个读数。我曾尝试使用getTime()
并System.currentTimeMillis();
在我打电话时调用时间getLatitude();
getLongitude();
,getAltitude();
但两种方式都有相同的延迟。由于我使用时间和位置进行计算,显然这种滞后是一个问题。
我想实现一个计时器,然后实现一个位置调用,但真的不知道如何实现它。
@Override
public void onLocationChanged(Location loc) {
/* Get current speed */
loc.getSpeed();
/* Assign speed value to double variable in meter/sec, convert to miles/hr, and convert to string*/
float currentSpeedInMeterPerSecond = loc.getSpeed();
float currentSpeed = currentSpeedInMeterPerSecond * 2.237f;
currentSpeedMPHInt = Math.round(currentSpeed);
currentSpeedString = Integer.toString(currentSpeedMPHInt);
/* Print out current speed */
TextView currentSpeedText = (TextView) findViewById(R.id.speed);
currentSpeedText.setText(currentSpeedString);
/* Get headwind, frontal area, drag coefficent (Cd), rolling coeffiecent (Cr) */
mySharedPreferences = getSharedPreferences(MY_PREFS, prefMode);
headWindString = mySharedPreferences.getString("keyheadWindString", "0");
headWind = new Float(headWindString);
frontalAreaString = mySharedPreferences.getString("keyfrontalAreaString", "6.6");
frontalArea = new Float(frontalAreaString);
dragCoefficentString = mySharedPreferences.getString("keydragCoefficentString", "1.15");
dragCoefficent = new Float(dragCoefficentString);
riderWeightString = mySharedPreferences.getString("keyriderWeightString", "205");
riderWeight = new Float(riderWeightString);
bikeWeightString = mySharedPreferences.getString("keybikeWeightString", "30");
bikeWeight = new Float(bikeWeightString);
rollingCoefficentString = mySharedPreferences.getString("keyrollingCoefficentString", "0.0075");
rollingCoefficent = new Float(rollingCoefficentString);
/* get current time and location and speed and altitude*/
System.currentTimeMillis();
loc.getLatitude();
loc.getLongitude();
loc.getAltitude();
/* Set start time and location */
if (i < j) {
i = i + 1;
time1 = System.currentTimeMillis();
altitudeStart = loc.getAltitude();
altitudeA = loc.getAltitude();
latA = loc.getLatitude();
logA = loc.getLongitude();
Location.distanceBetween(latA, logA, latB, logB, distance);
} else {
/* Get new location and time*/
timeNew = System.currentTimeMillis();
latB = loc.getLatitude();
logB = loc.getLongitude();
altitudeB = loc.getAltitude();
/* Get distance between point A and point B*/
Location.distanceBetween(latA, logA, latB, logB, distance);
distanceTraveledMeters = distance[0]; /* distance between A and B in meters*/
distanceTraveledMiles = distanceTraveledMeters * 0.0006213712f;
if (distanceTraveledMiles < 0.02f) {
distanceTraveledMiles = 0f;
}
/* Get and find time, total distance, average power , average speed, total Kcal, total elevation diff*/
timeinterval = timeNew - time1;
timeintervalSec = timeinterval / 1000f;
tripTimeTotalmin = tripTimeTotalmin + (timeintervalSec / 60f);
totalDistance = totalDistance + distanceTraveledMiles;
averageSpeed = totalDistance / (tripTimeTotalmin / 60f);
elevChange = (((altitudeB + altitudeA) / 2) - altitudeStart) * 3.2808d;
elevChangeFloat = (float) elevChange;
powerElevationChangeWattsAve = ((((riderWeight + bikeWeight) * 32.174f) * elevChangeFloat) / tripTimeTotalmin) * 0.00070234f;
intervalSpeedMPH = distanceTraveledMiles / (timeintervalSec / 3600f);
powerAirResistanceWattsAve = (0.5f * dragCoefficent * 0.075f * ((averageSpeed + headWind) * (averageSpeed + headWind)) * frontalArea * (averageSpeed + headWind)) * 0.13295f;
powerRollingResistanceWattsAve = rollingCoefficent * (riderWeight + bikeWeight) * 32.174f * averageSpeed * 0.062f;
powerTotalWattsAve = powerElevationChangeWattsAve + powerAirResistanceWattsAve + powerRollingResistanceWattsAve;
totalKcalBurned = (powerTotalWattsAve * tripTimeTotalmin * 0.01433f) / 0.24f;
mechBtuUsed = powerTotalWattsAve * tripTimeTotalmin * 0.056869f;
/*Round off values*/
totalDistance = totalDistance * 10f;
totalDistance = Math.round(totalDistance);
totalDistance = totalDistance / 10f;
tripTimeMin = Math.round(tripTimeTotalmin);
timeintervalSec = Math.round(timeintervalSec);
averageSpeedInt = Math.round(averageSpeed);
powerTotalWattsInt = Math.round(powerTotalWattsAve);
totalKcalBurnedInt = Math.round(totalKcalBurned);
intervalSpeedMPHInt = Math.round(intervalSpeedMPH);
mechBtuUsedInt = Math.round(mechBtuUsed);
elevChangeInt = Math.round(elevChangeFloat);
/*convert to stings*/
totalDistanceString = Float.toString(totalDistance);
tripTimeTotalminString = Integer.toString(tripTimeMin);
averageSpeedString = Integer.toString(averageSpeedInt);
powerTotalWattsString = Integer.toString(powerTotalWattsInt);
totalKcalBurnedString = Integer.toString(totalKcalBurnedInt);
mechBtuUsedString = Integer.toString(mechBtuUsedInt);
elevChangeString = Integer.toString(elevChangeInt);
/* Print out */
TextView totalDistanceText = (TextView) findViewById(R.id.distance);
totalDistanceText.setText(totalDistanceString);
TextView tripTimeTotalminText = (TextView) findViewById(R.id.triptime);
tripTimeTotalminText.setText(tripTimeTotalminString);
TextView averageSpeedText = (TextView) findViewById(R.id.avespeed);
averageSpeedText.setText(averageSpeedString);
TextView powerTotalWattsText = (TextView) findViewById(R.id.watts);
powerTotalWattsText.setText(powerTotalWattsString);
TextView totalKcalBurnedText = (TextView) findViewById(R.id.burnedkcal);
totalKcalBurnedText.setText(totalKcalBurnedString);
TextView mechBtuUsedText = (TextView) findViewById(R.id.mechbtu);
mechBtuUsedText.setText(mechBtuUsedString);
TextView elevChangeText = (TextView) findViewById(R.id.elevchange);
elevChangeText.setText(elevChangeString);
/*reset start time and location and interval elevation */
time1 = timeNew;
latA = latB;
logA = logB;
altitudeA = altitudeB;
/* check iterations*/
k = k + 1;
kString = Integer.toString(k);
TextView kStringText = (TextView) findViewById(R.id.k);
kStringText.setText(kString);
}
}