Skip to content

Commit 65e5513

Browse files
authored
Merge pull request #94 from google/jsclosure-patch
Fix bug #89 for js/closure
2 parents 9161476 + 4caed97 commit 65e5513

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
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

js/closure/openlocationcode_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ testSuite({
256256
assertEquals('Test ' + i, recovered, td[0]);
257257
}
258258
},
259+
testRecoveryNearPoles: function() {
260+
assertEquals("2CXXXXXX+XX", OpenLocationCode.recoverNearest("XXXXXX+XX", -81.0, 0.0));
261+
assertEquals("CFX22222+22", OpenLocationCode.recoverNearest("2222+22", 89.6, 0.0));
262+
},
259263
testValidity: function() {
260264
var tests = [
261265
// code,isValid,isShort,isFull

js/src/openlocationcode.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)