Skip to content

Commit 1f4d5c7

Browse files
committed
expose Dart integer encoding and conversion functions
1 parent 4b6b589 commit 1f4d5c7

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

dart/lib/src/open_location_code.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)