Skip to content

Commit f88bf38

Browse files
authored
Update open_location_code.dart
1 parent 8e1c022 commit f88bf38

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

dart/lib/src/open_location_code.dart

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ num clipLatitude(num latitude) => latitude.clamp(-90.0, 90.0);
136136
/// columns than rows.
137137
int computeLatitudePrecision(int codeLength) {
138138
if (codeLength <= 10) {
139-
return pow(20, (codeLength ~/ -2) + 2);
139+
return pow(encodingBase, (codeLength ~/ -2) + 2);
140140
}
141-
return pow(20, -3) ~/ pow(gridRows, codeLength - 10);
141+
return pow(encodingBase, -3) ~/ pow(gridRows, codeLength - 10);
142142
}
143143

144144
/// Normalize a [longitude] into the range -180 to 180, not including 180.
@@ -322,9 +322,9 @@ String recoverNearest(
322322
// Compute the number of digits we need to recover.
323323
var paddingLength = separatorPosition - shortCode.indexOf(separator);
324324
// The resolution (height and width) of the padded area in degrees.
325-
var resolution = pow(20, 2 - (paddingLength / 2));
325+
var resolution = pow(encodingBase, 2 - (paddingLength / 2));
326326
// Distance from the center to an edge (in degrees).
327-
var areaToEdge = resolution / 2.0;
327+
var halfResolution = resolution / 2.0;
328328

329329
// Use the reference location to pad the supplied short code and decode it.
330330
var codeArea = decode(
@@ -334,15 +334,17 @@ String recoverNearest(
334334
var centerLongitude = codeArea.center.longitude;
335335

336336
// How many degrees latitude is the code from the reference? If it is more
337-
// than half the resolution, we need to move it east or west.
338-
var degreesDifference = centerLatitude - referenceLatitude;
339-
if (degreesDifference > areaToEdge) {
340-
// If the center of the short code is more than half a cell east,
341-
// then the best match will be one position west.
337+
// than half the resolution, we need to move it north or south but keep it
338+
// within -90 to 90 degrees.
339+
if (referenceLatitude + halfResolution < centerLatitude &&
340+
centerLatitude - resolution >= -LATITUDE_MAX_) {
341+
// If the proposed code is more than half a cell north of the reference location,
342+
// it's too far, and the best match will be one cell south.
342343
centerLatitude -= resolution;
343-
} else if (degreesDifference < -areaToEdge) {
344-
// If the center of the short code is more than half a cell west,
345-
// then the best match will be one position east.
344+
} else if (referenceLatitude - halfResolution > centerLatitude &&
345+
centerLatitude + resolution <= LATITUDE_MAX_) {
346+
// If the proposed code is more than half a cell south of the reference location,
347+
// it's too far, and the best match will be one cell north.
346348
centerLatitude += resolution;
347349
}
348350

@@ -354,6 +356,13 @@ String recoverNearest(
354356
centerLongitude += resolution;
355357
}
356358

359+
// How many degrees longitude is the code from the reference?
360+
if (referenceLongitude + halfResolution < centerLongitude) {
361+
centerLongitude -= resolution;
362+
} else if (referenceLongitude - halfResolution > centerLongitude) {
363+
centerLongitude += resolution;
364+
}
365+
357366
return encode(centerLatitude, centerLongitude,
358367
codeLength: codeArea.codeLength);
359368
}

0 commit comments

Comments
 (0)