@@ -259,11 +259,13 @@ String encode(num latitude, num longitude, {int codeLength = pairCodeLength}) {
259259 throw ArgumentError ('Invalid Open Location Code length: $codeLength ' );
260260 }
261261 codeLength = min (maxDigitCount, codeLength);
262+ var integers = locationToIntegers (latitude, longitude);
263+ return encodeIntegers (integers[0 ], integers[1 ], codeLength);
264+ }
262265
263- // This approach converts each value to an integer after multiplying it by
264- // the final precision. This allows us to use only integer operations, so
265- // avoiding any accumulation of floating point representation errors.
266-
266+ // Convert latitude and longitude in degrees to the integer values needed for
267+ // reliable conversions.
268+ List <int > locationToIntegers (num latitude, num longitude) {
267269 // Convert latitude into a positive integer clipped into the range 0-(just
268270 // under 180*2.5e7). Latitude 90 needs to be adjusted to be just less, so the
269271 // returned code can also be decoded.
@@ -286,7 +288,11 @@ String encode(num latitude, num longitude, {int codeLength = pairCodeLength}) {
286288 } else if (lngVal >= 2 * longitudeMax * finalLngPrecision) {
287289 lngVal = lngVal % (2 * longitudeMax * finalLngPrecision);
288290 }
291+ return [latVal, lngVal];
292+ }
289293
294+ /// Encode a location into an Open Location Code.
295+ String encodeIntegers (int latVal, int lngVal, int codeLength) {
290296 List <String > code = List <String >.filled (maxDigitCount + 1 , '' );
291297 code[separatorPosition] = separator;
292298
0 commit comments