@@ -136,9 +136,9 @@ num clipLatitude(num latitude) => latitude.clamp(-90.0, 90.0);
136
136
/// columns than rows.
137
137
int computeLatitudePrecision (int codeLength) {
138
138
if (codeLength <= 10 ) {
139
- return pow (20 , (codeLength ~ / - 2 ) + 2 );
139
+ return pow (encodingBase , (codeLength ~ / - 2 ) + 2 );
140
140
}
141
- return pow (20 , - 3 ) ~ / pow (gridRows, codeLength - 10 );
141
+ return pow (encodingBase , - 3 ) ~ / pow (gridRows, codeLength - 10 );
142
142
}
143
143
144
144
/// Normalize a [longitude] into the range -180 to 180, not including 180.
@@ -322,9 +322,9 @@ String recoverNearest(
322
322
// Compute the number of digits we need to recover.
323
323
var paddingLength = separatorPosition - shortCode.indexOf (separator);
324
324
// 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 ));
326
326
// Distance from the center to an edge (in degrees).
327
- var areaToEdge = resolution / 2.0 ;
327
+ var halfResolution = resolution / 2.0 ;
328
328
329
329
// Use the reference location to pad the supplied short code and decode it.
330
330
var codeArea = decode (
@@ -334,23 +334,24 @@ String recoverNearest(
334
334
var centerLongitude = codeArea.center.longitude;
335
335
336
336
// 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 >= - latitudeMax) {
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.
342
343
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 <= latitudeMax) {
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.
346
348
centerLatitude += resolution;
347
349
}
348
350
349
351
// How many degrees longitude is the code from the reference?
350
- degreesDifference = codeArea.center.longitude - referenceLongitude;
351
- if (degreesDifference > areaToEdge) {
352
+ if (referenceLongitude + halfResolution < centerLongitude) {
352
353
centerLongitude -= resolution;
353
- } else if (degreesDifference < - areaToEdge ) {
354
+ } else if (referenceLongitude - halfResolution > centerLongitude ) {
354
355
centerLongitude += resolution;
355
356
}
356
357
0 commit comments