@@ -408,30 +408,30 @@ function recoverNearest(
408
408
// The resolution (height and width) of the padded area in degrees.
409
409
var resolution = Math . pow ( 20 , 2 - ( paddingLength / 2 ) ) ;
410
410
// Distance from the center to an edge (in degrees).
411
- var areaToEdge = resolution / 2.0 ;
411
+ var halfResolution = resolution / 2.0 ;
412
412
413
413
// Use the reference location to pad the supplied short code and decode it.
414
414
var codeArea = decode (
415
- encode ( referenceLatitude , referenceLongitude ) . substr ( 0 , paddingLength ) +
416
- shortCode ) ;
415
+ encode ( referenceLatitude , referenceLongitude ) . substr ( 0 , paddingLength ) + shortCode ) ;
417
416
// How many degrees latitude is the code from the reference? If it is more
418
- // than half the resolution, we need to move it east or west.
419
- var degreesDifference = codeArea . latitudeCenter - referenceLatitude ;
420
- if ( degreesDifference > areaToEdge ) {
421
- // If the center of the short code is more than half a cell east,
422
- // then the best match will be one position west.
417
+ // than half the resolution, we need to move it north or south but keep it
418
+ // within -90 to 90 degrees.
419
+ if ( referenceLatitude + halfResolution < codeArea . latitudeCenter &&
420
+ codeArea . latitudeCenter - resolution >= - LATITUDE_MAX_ ) {
421
+ // If the proposed code is more than half a cell north of the reference location,
422
+ // it's too far, and the best match will be one cell south.
423
423
codeArea . latitudeCenter -= resolution ;
424
- } else if ( degreesDifference < - areaToEdge ) {
425
- // If the center of the short code is more than half a cell west,
426
- // then the best match will be one position east.
424
+ } else if ( referenceLatitude - halfResolution > codeArea . latitudeCenter &&
425
+ codeArea . latitudeCenter + resolution <= LATITUDE_MAX_ ) {
426
+ // If the proposed code is more than half a cell south of the reference location,
427
+ // it's too far, and the best match will be one cell north.
427
428
codeArea . latitudeCenter += resolution ;
428
429
}
429
430
430
431
// How many degrees longitude is the code from the reference?
431
- degreesDifference = codeArea . longitudeCenter - referenceLongitude ;
432
- if ( degreesDifference > areaToEdge ) {
432
+ if ( referenceLongitude + halfResolution < codeArea . longitudeCenter ) {
433
433
codeArea . longitudeCenter -= resolution ;
434
- } else if ( degreesDifference < - areaToEdge ) {
434
+ } else if ( referenceLongitude - halfResolution > codeArea . longitudeCenter ) {
435
435
codeArea . longitudeCenter += resolution ;
436
436
}
437
437
0 commit comments