Troubleshooting guide
211
14: Using location information
* Rounds off a given double to the provided number of decimal places
* @param d the double to round off
* @param decimal the number of decimal places to retain
* @return a double with the number of decimal places specified
*/
private static double round(double d, int decimal)
{
double powerOfTen = 1;
while (decimal-- > 0)
{
powerOfTen *= 10.0;
}
double d1 = d * powerOfTen;
int d1asint = (int)d1; //clip the decimal portion away and cache the cast, this is
a costly transformation
double d2 = d1 - d1asint; //get the remainder of the double
//is the remainder > 0.5? if so, round up, otherwise round down (lump in .5 with >
case for simplicity)
return ( d2 >= 0.5 ? (d1asint + 1)/powerOfTen : (d1asint)/powerOfTen);
}
/**
* Implementation of the LocationListener interface.
*/
private class LocationListenerImpl implements LocationListener {
// Members. --------------------------------------------------------------
private int captureCount;
private int sendCount;
// Methods. --------------------------------------------------------------
public void locationUpdated(LocationProvider provider, Location location) {
if(location.isValid()) {
float heading = location.getCourse();
double longitude = location.getQualifiedCoordinates().getLongitude();
double latitude = location.getQualifiedCoordinates().getLatitude();
float altitude = location.getQualifiedCoordinates().getAltitude();
float speed = location.getSpeed();
// Horizontal distance.
float horizontalDistance = speed * _interval;
_horizontalDistance += horizontalDistance;
// Horizontal distance for this waypoint.
_wayHorizontalDistance += horizontalDistance;
// Distance over the current interval.
float totalDist = 0;
// Moving average grade.
for(int i = 0; i < GRADE_INTERVAL - 1; ++i) {
_altitudes[i] = _altitudes[i+1];
_horizontalDistances[i] = _horizontalDistances[i+1];
totalDist = totalDist + _horizontalDistances[i];
}
_altitudes[GRADE_INTERVAL-1] = altitude;
_horizontalDistances[GRADE_INTERVAL-1] = speed*_interval;
totalDist= totalDist + _horizontalDistances[GRADE_INTERVAL-1];
float grade = (totalDist==0.0F)? Float.NaN : ( (_altitudes[4] -
_altitudes[0]) * 100/totalDist);