Android Location class with U.S. customary units (instead of metric)


I’m a big proponent of going metric; I’ve never fully understood why we don’t just do it.  Then again, I’d also like the entire world to be on UTC time.  But since we insist on using our own bass-ackward units of measure, I needed an Android Location class that returned U.S. customary units instead of metric.

So, here goes…

import android.location.Location;

public class CLocation extends Location
{
  private boolean bUseMetricUnits = false;

  public CLocation(Location location)
  {
    this(location, true);
  }

  public CLocation(Location location, boolean bUseMetricUnits)
  {
    super(location);
    this.bUseMetricUnits = bUseMetricUnits;
  }

  public boolean getUseMetricUnits()
  {
    return this.bUseMetricUnits;
  }

  public void setUseMetricUnits(boolean bUseMetricUnits)
  {
    this.bUseMetricUnits = bUseMetricUnits;
  }

  @Override
  public float distanceTo(Location dest)
  {
    float nDistance = super.distanceTo(dest);

    if (!this.getUseMetricUnits())
    {
      // Convert meters to feet
      nDistance = nDistance * 3.28083989501312f;
    }

    return nDistance;
  }

  @Override
  public float getAccuracy()
  {
    float nAccuracy = super.getAccuracy();

    if (!this.getUseMetricUnits())
    {
      // Convert meters to feet
      nAccuracy = nAccuracy * 3.28083989501312f;
    }

    return nAccuracy;
  }

  @Override
  public double getAltitude()
  {
    double nAltitude = super.getAltitude();

    if (!this.getUseMetricUnits())
    {
      // Convert meters to feet
      nAltitude = nAltitude * 3.28083989501312d;
    }

    return nAltitude;
  }

  @Override
  public float getSpeed()
  {
    float nSpeed = super.getSpeed();

    if (!this.getUseMetricUnits())
    {
      // Convert meters/second to miles/hour
      nSpeed = nSpeed * 2.2369362920544f;
    }

    return nSpeed;
  }
}

This class really isn’t anything special, it just allows you to toggle which units you would like to use:

  • U.S. Customary Units:  feet and miles/hour
  • Metric:  meters and meters/second

Please, take it and make it better :)


Leave a Reply