public final class OpenLocationCode extends Object
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()
Modifier and Type | Class and Description |
---|---|
static class |
OpenLocationCode.CodeArea
Coordinates of a decoded Open Location Code.
|
Modifier and Type | Field and Description |
---|---|
static String |
CODE_ALPHABET |
static int |
CODE_PRECISION_NORMAL |
static int |
MAX_DIGIT_COUNT |
static char |
PADDING_CHARACTER |
static char |
SEPARATOR |
Constructor and Description |
---|
OpenLocationCode(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.
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(double latitude,
double longitude)
Returns whether the bounding box specified by the Open Location Code contains provided point.
|
OpenLocationCode.CodeArea |
decode()
Decodes
OpenLocationCode object into OpenLocationCode.CodeArea object encapsulating
latitude/longitude bounding box. |
static OpenLocationCode.CodeArea |
decode(String code)
Decodes code representing Open Location Code into
OpenLocationCode.CodeArea object encapsulating
latitude/longitude bounding box. |
static String |
encode(double latitude,
double longitude)
Encodes latitude/longitude into 10 digit Open Location Code.
|
static String |
encode(double latitude,
double longitude,
int codeLength)
Encodes latitude/longitude into Open Location Code of the provided length.
|
boolean |
equals(Object o) |
String |
getCode()
Returns the string representation of the code.
|
int |
hashCode() |
boolean |
isFull()
Returns whether this
OpenLocationCode is a full Open Location Code. |
static boolean |
isFull(String code)
Returns whether the provided Open Location Code is a full Open Location Code.
|
static boolean |
isFullCode(String code)
Returns if the code is a valid full Open Location Code.
|
static boolean |
isPadded(String code)
Returns whether the provided Open Location Code is a padded Open Location Code, meaning that it
contains less than 8 valid digits.
|
boolean |
isShort()
Returns whether this
OpenLocationCode is a short Open Location Code. |
static boolean |
isShort(String code)
Returns whether the provided Open Location Code is a short Open Location Code.
|
static boolean |
isShortCode(String code)
Returns if the code is a valid short Open Location Code.
|
static boolean |
isValidCode(String code)
Returns whether the provided string is a valid Open Location code.
|
OpenLocationCode |
recover(double referenceLatitude,
double referenceLongitude)
Returns an
OpenLocationCode object representing a full Open Location Code from this
(short) Open Location Code, given the reference location. |
OpenLocationCode |
shorten(double referenceLatitude,
double referenceLongitude)
Returns short
OpenLocationCode from the full Open Location Code created by removing
four or six digits, depending on the provided reference point. |
String |
toString() |
public static final int CODE_PRECISION_NORMAL
public static final String CODE_ALPHABET
public static final char SEPARATOR
public static final char PADDING_CHARACTER
public static final int MAX_DIGIT_COUNT
public OpenLocationCode(String code)
code
- A valid OLC code. Can be a full code or a shortened code.IllegalArgumentException
- when the passed code is not valid.public OpenLocationCode(double latitude, double longitude, int codeLength)
latitude
- The latitude in decimal degrees.longitude
- The longitude in decimal degrees.codeLength
- The desired number of digits in the code.IllegalArgumentException
- if the code length is not valid.public OpenLocationCode(double latitude, double longitude)
latitude
- The latitude in decimal degrees.longitude
- The longitude in decimal degrees.public String getCode()
public static String encode(double latitude, double longitude)
latitude
- The latitude in decimal degrees.longitude
- The longitude in decimal degrees.public static String encode(double latitude, double longitude, int codeLength)
latitude
- The latitude in decimal degrees.longitude
- The longitude in decimal degrees.codeLength
- The number of digits in the returned code.public OpenLocationCode.CodeArea decode()
OpenLocationCode
object into OpenLocationCode.CodeArea
object encapsulating
latitude/longitude bounding box.public static OpenLocationCode.CodeArea decode(String code) throws IllegalArgumentException
OpenLocationCode.CodeArea
object encapsulating
latitude/longitude bounding box.code
- Open Location Code to be decoded.IllegalArgumentException
- if the provided code is not a valid Open Location Code.public boolean isFull()
OpenLocationCode
is a full Open Location Code.public static boolean isFull(String code) throws IllegalArgumentException
code
- The code to check.IllegalArgumentException
public boolean isShort()
OpenLocationCode
is a short Open Location Code.public static boolean isShort(String code) throws IllegalArgumentException
code
- The code to check.IllegalArgumentException
public static boolean isPadded(String code) throws IllegalArgumentException
code
- The code to check.IllegalArgumentException
public OpenLocationCode shorten(double referenceLatitude, double referenceLongitude)
OpenLocationCode
from 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.referenceLatitude
- Degrees.referenceLongitude
- Degrees.public OpenLocationCode recover(double referenceLatitude, double referenceLongitude)
OpenLocationCode
object representing a full Open Location Code from this
(short) Open Location Code, given the reference location.referenceLatitude
- Degrees.referenceLongitude
- Degrees.public boolean contains(double latitude, double longitude)
latitude
- Degrees.longitude
- Degrees.public static boolean isValidCode(String code)
code
- The code to check.public static boolean isFullCode(String code)
code
- The code to check.public static boolean isShortCode(String code)
code
- The code to check.Copyright © 2017–2023 DNA Evolutions GmbH. All rights reserved.