Class OpenLocationCode
Open Location Codes are short, ~10 character codes that can be used instead of street addresses. The codes can be generated and decoded offline, and use a reduced character set that minimises the chance of codes including words.
This provides both object and static methods.
Create an object with: OpenLocationCode code = new OpenLocationCode("7JVW52GR+2V"); OpenLocationCode code = new OpenLocationCode("52GR+2V"); OpenLocationCode code = new OpenLocationCode(27.175063, 78.042188); OpenLocationCode code = new OpenLocationCode(27.175063, 78.042188, 11);
Once you have a code object, you can apply the other methods to it, such as to shorten: code.shorten(27.176, 78.05)
Recover the nearest match (if the code was a short code): code.recover(27.176, 78.05)
Or decode a code into its coordinates, returning a CodeArea object. code.decode()
- Author:
- Jiri Semecky, Doug Rinckes
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCoordinates of a decoded Open Location Code. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final intstatic final intstatic final charstatic final char -
Constructor Summary
ConstructorsConstructorDescriptionOpenLocationCode(double latitude, double longitude) Creates Open Location Code with the default precision length.OpenLocationCode(double latitude, double longitude, int codeLength) Creates Open Location Code.OpenLocationCode(String code) Creates Open Location Code object for the provided code. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontains(double latitude, double longitude) Returns whether the bounding box specified by the Open Location Code contains provided point.decode()DecodesOpenLocationCodeobject intoOpenLocationCode.CodeAreaobject encapsulating latitude/longitude bounding box.static OpenLocationCode.CodeAreaDecodes code representing Open Location Code intoOpenLocationCode.CodeAreaobject encapsulating latitude/longitude bounding box.static Stringencode(double latitude, double longitude) Encodes latitude/longitude into 10 digit Open Location Code.static Stringencode(double latitude, double longitude, int codeLength) Encodes latitude/longitude into Open Location Code of the provided length.booleangetCode()Returns the string representation of the code.inthashCode()booleanisFull()Returns whether thisOpenLocationCodeis a full Open Location Code.static booleanReturns whether the provided Open Location Code is a full Open Location Code.static booleanisFullCode(String code) Returns if the code is a valid full Open Location Code.static booleanReturns whether the provided Open Location Code is a padded Open Location Code, meaning that it contains less than 8 valid digits.booleanisShort()Returns whether thisOpenLocationCodeis a short Open Location Code.static booleanReturns whether the provided Open Location Code is a short Open Location Code.static booleanisShortCode(String code) Returns if the code is a valid short Open Location Code.static booleanisValidCode(String code) Returns whether the provided string is a valid Open Location code.recover(double referenceLatitude, double referenceLongitude) Returns anOpenLocationCodeobject representing a full Open Location Code from this (short) Open Location Code, given the reference location.shorten(double referenceLatitude, double referenceLongitude) Returns shortOpenLocationCodefrom the full Open Location Code created by removing four or six digits, depending on the provided reference point.toString()
-
Field Details
-
CODE_PRECISION_NORMAL
public static final int CODE_PRECISION_NORMAL- See Also:
-
CODE_ALPHABET
- See Also:
-
SEPARATOR
public static final char SEPARATOR- See Also:
-
PADDING_CHARACTER
public static final char PADDING_CHARACTER- See Also:
-
MAX_DIGIT_COUNT
public static final int MAX_DIGIT_COUNT- See Also:
-
-
Constructor Details
-
OpenLocationCode
Creates Open Location Code object for the provided code.- Parameters:
code- A valid OLC code. Can be a full code or a shortened code.- Throws:
IllegalArgumentException- when the passed code is not valid.
-
OpenLocationCode
public OpenLocationCode(double latitude, double longitude, int codeLength) Creates Open Location Code.- Parameters:
latitude- The latitude in decimal degrees.longitude- The longitude in decimal degrees.codeLength- The desired number of digits in the code.- Throws:
IllegalArgumentException- if the code length is not valid.
-
OpenLocationCode
public OpenLocationCode(double latitude, double longitude) Creates Open Location Code with the default precision length.- Parameters:
latitude- The latitude in decimal degrees.longitude- The longitude in decimal degrees.
-
-
Method Details
-
getCode
Returns the string representation of the code.- Returns:
- The code.
-
encode
Encodes latitude/longitude into 10 digit Open Location Code. This method is equivalent to creating the OpenLocationCode object and getting the code from it.- Parameters:
latitude- The latitude in decimal degrees.longitude- The longitude in decimal degrees.- Returns:
- The code.
-
encode
Encodes latitude/longitude into Open Location Code of the provided length. This method is equivalent to creating the OpenLocationCode object and getting the code from it.- Parameters:
latitude- The latitude in decimal degrees.longitude- The longitude in decimal degrees.codeLength- The number of digits in the returned code.- Returns:
- The code.
-
decode
DecodesOpenLocationCodeobject intoOpenLocationCode.CodeAreaobject encapsulating latitude/longitude bounding box.- Returns:
- A CodeArea object.
-
decode
Decodes code representing Open Location Code intoOpenLocationCode.CodeAreaobject encapsulating latitude/longitude bounding box.- Parameters:
code- Open Location Code to be decoded.- Returns:
- A CodeArea object.
- Throws:
IllegalArgumentException- if the provided code is not a valid Open Location Code.
-
isFull
public boolean isFull()Returns whether thisOpenLocationCodeis a full Open Location Code.- Returns:
- True if it is a full code.
-
isFull
Returns whether the provided Open Location Code is a full Open Location Code.- Parameters:
code- The code to check.- Returns:
- True if it is a full code.
- Throws:
IllegalArgumentException
-
isShort
public boolean isShort()Returns whether thisOpenLocationCodeis a short Open Location Code.- Returns:
- True if it is short.
-
isShort
Returns whether the provided Open Location Code is a short Open Location Code.- Parameters:
code- The code to check.- Returns:
- True if it is short.
- Throws:
IllegalArgumentException
-
isPadded
Returns whether the provided Open Location Code is a padded Open Location Code, meaning that it contains less than 8 valid digits.- Parameters:
code- The code to check.- Returns:
- True if it is padded.
- Throws:
IllegalArgumentException
-
shorten
Returns shortOpenLocationCodefrom the full Open Location Code created by removing four or six digits, depending on the provided reference point. It removes as many digits as possible.- Parameters:
referenceLatitude- Degrees.referenceLongitude- Degrees.- Returns:
- A short code if possible.
-
recover
Returns anOpenLocationCodeobject representing a full Open Location Code from this (short) Open Location Code, given the reference location.- Parameters:
referenceLatitude- Degrees.referenceLongitude- Degrees.- Returns:
- The nearest matching full code.
-
contains
public boolean contains(double latitude, double longitude) Returns whether the bounding box specified by the Open Location Code contains provided point.- Parameters:
latitude- Degrees.longitude- Degrees.- Returns:
- True if the coordinates are contained by the code.
-
equals
-
hashCode
public int hashCode() -
toString
-
isValidCode
Returns whether the provided string is a valid Open Location code.- Parameters:
code- The code to check.- Returns:
- True if it is a valid full or short code.
-
isFullCode
Returns if the code is a valid full Open Location Code.- Parameters:
code- The code to check.- Returns:
- True if it is a valid full code.
-
isShortCode
Returns if the code is a valid short Open Location Code.- Parameters:
code- The code to check.- Returns:
- True if it is a valid short code.
-