Skip to content

Commit c28be98

Browse files
authored
Update openlocationcode.js
1 parent 9fb59e2 commit c28be98

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

js/closure/openlocationcode.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -408,30 +408,30 @@ function recoverNearest(
408408
// The resolution (height and width) of the padded area in degrees.
409409
var resolution = Math.pow(20, 2 - (paddingLength / 2));
410410
// Distance from the center to an edge (in degrees).
411-
var areaToEdge = resolution / 2.0;
411+
var halfResolution = resolution / 2.0;
412412

413413
// Use the reference location to pad the supplied short code and decode it.
414414
var codeArea = decode(
415-
encode(referenceLatitude, referenceLongitude).substr(0, paddingLength) +
416-
shortCode);
415+
encode(referenceLatitude, referenceLongitude).substr(0, paddingLength) + shortCode);
417416
// 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.
423423
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.
427428
codeArea.latitudeCenter += resolution;
428429
}
429430

430431
// 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) {
433433
codeArea.longitudeCenter -= resolution;
434-
} else if (degreesDifference < -areaToEdge) {
434+
} else if (referenceLongitude - halfResolution > codeArea.longitudeCenter) {
435435
codeArea.longitudeCenter += resolution;
436436
}
437437

0 commit comments

Comments
 (0)