Skip to content

Commit d4048c9

Browse files
committed
Merge pull request #46 from zongweil/precomputeLatitudePrecision
Make latitude precisions for digits static constants.
2 parents 3f16a8d + d219a82 commit d4048c9

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

java/com/google/openlocationcode/OpenLocationCode.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public final class OpenLocationCode {
1919
private static final BigDecimal BD_20 = new BigDecimal(20);
2020
private static final BigDecimal BD_90 = new BigDecimal(90);
2121
private static final BigDecimal BD_180 = new BigDecimal(180);
22+
private static final double LATITUDE_PRECISION_8_DIGITS = computeLatitudePrecision(8) / 4;
23+
private static final double LATITUDE_PRECISION_6_DIGITS = computeLatitudePrecision(6) / 4;
24+
private static final double LATITUDE_PRECISION_4_DIGITS = computeLatitudePrecision(4) / 4;
2225

2326
private static final char[] ALPHABET = "23456789CFGHJMPQRVWX".toCharArray();
2427
private static final Map<Character, Integer> CHARACTER_TO_INDEX = new HashMap<>();
@@ -309,13 +312,13 @@ public OpenLocationCode shorten(double referenceLatitude, double referenceLongit
309312
double latitudeDiff = Math.abs(referenceLatitude - codeArea.getCenterLatitude());
310313
double longitudeDiff = Math.abs(referenceLongitude - codeArea.getCenterLongitude());
311314

312-
if (latitudeDiff < 0.000625 && longitudeDiff < 0.000625) {
315+
if (latitudeDiff < LATITUDE_PRECISION_8_DIGITS && longitudeDiff < LATITUDE_PRECISION_8_DIGITS) {
313316
return new OpenLocationCode(code.substring(8));
314317
}
315-
if (latitudeDiff < 0.0125 && longitudeDiff < 0.0125) {
318+
if (latitudeDiff < LATITUDE_PRECISION_6_DIGITS && longitudeDiff < LATITUDE_PRECISION_6_DIGITS) {
316319
return new OpenLocationCode(code.substring(6));
317320
}
318-
if (latitudeDiff < 0.25 && longitudeDiff < 0.25) {
321+
if (latitudeDiff < LATITUDE_PRECISION_4_DIGITS && longitudeDiff < LATITUDE_PRECISION_4_DIGITS) {
319322
return new OpenLocationCode(code.substring(4));
320323
}
321324
throw new IllegalArgumentException(

0 commit comments

Comments
 (0)