Google is committed to advancing racial equity for Black communities. See how.

Distance

public final class Distance
extends Object

java.lang.Object
   ↳ com.google.android.libraries.car.app.model.Distance


Represents a distance value and how it should be displayed in the UI.

Summary

Constants

int UNIT_FEET

Feet unit.

int UNIT_KILOMETERS

Kilometer unit.

int UNIT_KILOMETERS_P1

Kilometer unit with the additional requirement that distances of this type be displayed with at least 1 digit of precision after the decimal point (for example, 2.0).

int UNIT_METERS

Meter unit.

int UNIT_MILES

Miles unit.

int UNIT_MILES_P1

Mile unit with the additional requirement that distances of this type be displayed with at least 1 digit of precision after the decimal point (for example, 2.0).

int UNIT_YARDS

Yards unit.

Public methods

static Distance create(double displayDistance, int displayUnit)

Constructs a new instance of a Distance.

boolean equals(Object other)
double getDisplayDistance()

Returns the distance measured in the unit indicated at getDisplayUnit().

int getDisplayUnit()

Returns the unit that should be used to display the distance value, adjusted to the current user's locale and location.

int hashCode()
String toString()

Inherited methods

Constants

UNIT_FEET

public static final int UNIT_FEET

Feet unit.

Constant Value: 6 (0x00000006)

UNIT_KILOMETERS

public static final int UNIT_KILOMETERS

Kilometer unit.

Constant Value: 2 (0x00000002)

UNIT_KILOMETERS_P1

public static final int UNIT_KILOMETERS_P1

Kilometer unit with the additional requirement that distances of this type be displayed with at least 1 digit of precision after the decimal point (for example, 2.0).

Constant Value: 3 (0x00000003)

UNIT_METERS

public static final int UNIT_METERS

Meter unit.

Constant Value: 1 (0x00000001)

UNIT_MILES

public static final int UNIT_MILES

Miles unit.

Constant Value: 4 (0x00000004)

UNIT_MILES_P1

public static final int UNIT_MILES_P1

Mile unit with the additional requirement that distances of this type be displayed with at least 1 digit of precision after the decimal point (for example, 2.0).

Constant Value: 5 (0x00000005)

UNIT_YARDS

public static final int UNIT_YARDS

Yards unit.

Constant Value: 7 (0x00000007)

Public methods

create

public static Distance create (double displayDistance, 
                int displayUnit)

Constructs a new instance of a Distance.

Units with precision requirements, UNIT_KILOMETERS_P1 and UNIT_MILES_P1, will always show one decimal digit. All other units will show a decimal digit if needed but will not if the distance is a whole number.

Examples

A display distance of 1.0 with a display unit of UNIT_KILOMETERS will display "1 km", whereas if the display unit is UNIT_KILOMETERS_P1 it will display "1.0 km". Note the "km" part of the string in this example depends on the locale the host is configured with.

A display distance of 1.46 however will display "1.4 km" for both UNIT_KILOMETERS and UNIT_KILOMETERS display units.

UNIT_KILOMETERS_P1 and UNIT_MILES_P1 can be used to provide consistent digit placement for a sequence of distances. For example, as the user is driving and the next turn distance changes, using UNIT_KILOMETERS_P1 will produce: "2.5 km", "2.0 km", "1.5 km", "1.0 km", and so on.

Parameters
displayDistance double: the distance to display, in the units specified in displayUnit. See getDisplayDistance().

displayUnit int: the unit of distance to use when displaying the value in displayUnit. This should be one of the UNIT_* static constants defined in this class. See getDisplayUnit().

Returns
Distance

Throws
IllegalArgumentException if displayDistance is negative.

equals

public boolean equals (Object other)

Parameters
other Object

Returns
boolean

getDisplayDistance

public double getDisplayDistance ()

Returns the distance measured in the unit indicated at getDisplayUnit().

This distance is for display purposes only and it might be a rounded representation of the actual distance. For example, a distance of 1000 meters could be shown in the following ways:

  • Display unit of UNIT_METERS and distance of 1000000, resulting in a display of "1000 m".
  • Display unit of UNIT_KILOMETERS and distance of 1000, resulting in a display of "1 km".
  • Display unit of UNIT_KILOMETERS_P1 and distance of 1000, resulting in a display of "1.0 km".

Returns
double

getDisplayUnit

public int getDisplayUnit ()

Returns the unit that should be used to display the distance value, adjusted to the current user's locale and location. This should match the unit used in getDisplayDistance().

Returns
int

hashCode

public int hashCode ()

Returns
int

toString

public String toString ()

Returns
String