@@ -194,14 +194,6 @@ public OpenLocationCode(double latitude, double longitude, int codeLength) {
194
194
if (codeLength < PAIR_CODE_LENGTH && codeLength % 2 == 1 || codeLength < MIN_DIGIT_COUNT ) {
195
195
throw new IllegalArgumentException ("Illegal code length " + codeLength );
196
196
}
197
- // Ensure that latitude and longitude are valid.
198
- latitude = clipLatitude (latitude );
199
- longitude = normalizeLongitude (longitude );
200
-
201
- // Latitude 90 needs to be adjusted to be just less, so the returned code can also be decoded.
202
- if (latitude == LATITUDE_MAX ) {
203
- latitude = latitude - 0.9 * computeLatitudePrecision (codeLength );
204
- }
205
197
206
198
// Store the code - we build it in reverse and reorder it afterwards.
207
199
StringBuilder revCodeBuilder = new StringBuilder ();
@@ -211,12 +203,24 @@ public OpenLocationCode(double latitude, double longitude, int codeLength) {
211
203
// the final precision. This allows us to use only integer operations, so
212
204
// avoiding any accumulation of floating point representation errors.
213
205
214
- // Multiply values by their precision and convert to positive. Rounding
215
- // avoids/minimises errors due to floating point precision.
216
- long latVal =
217
- (long ) (Math .round ((latitude + LATITUDE_MAX ) * LAT_INTEGER_MULTIPLIER * 1e6 ) / 1e6 );
218
- long lngVal =
219
- (long ) (Math .round ((longitude + LONGITUDE_MAX ) * LNG_INTEGER_MULTIPLIER * 1e6 ) / 1e6 );
206
+ long latVal = (long ) Math .round (latitude * LAT_INTEGER_MULTIPLIER );
207
+ long lngVal = (long ) Math .round (longitude * LNG_INTEGER_MULTIPLIER );
208
+
209
+ // Clip and normalise values.
210
+ latVal += LATITUDE_MAX * LAT_INTEGER_MULTIPLIER ;
211
+ if (latVal < 0 ) {
212
+ latVal = 0 ;
213
+ } else if (latVal >= 2 * LATITUDE_MAX * LAT_INTEGER_MULTIPLIER ) {
214
+ latVal = 2 * LATITUDE_MAX * LAT_INTEGER_MULTIPLIER - 1 ;
215
+ }
216
+ lngVal += LONGITUDE_MAX * LNG_INTEGER_MULTIPLIER ;
217
+ if (lngVal < 0 ) {
218
+ lngVal =
219
+ lngVal % (2 * LONGITUDE_MAX * LNG_INTEGER_MULTIPLIER )
220
+ + 2 * LONGITUDE_MAX * LNG_INTEGER_MULTIPLIER ;
221
+ } else if (lngVal >= 2 * LONGITUDE_MAX * LNG_INTEGER_MULTIPLIER ) {
222
+ lngVal = lngVal % (2 * LONGITUDE_MAX * LNG_INTEGER_MULTIPLIER );
223
+ }
220
224
221
225
// Compute the grid part of the code if necessary.
222
226
if (codeLength > PAIR_CODE_LENGTH ) {
0 commit comments