@@ -363,28 +363,32 @@ function encode(latitude, longitude, optLength) {
363
363
'IllegalArgumentException: Invalid Plus Code length' ) ;
364
364
}
365
365
optLength = Math . min ( optLength , MAX_CODE_LEN ) ;
366
- // Ensure that latitude and longitude are valid.
367
- latitude = clipLatitude ( latitude ) ;
368
- longitude = normalizeLongitude ( longitude ) ;
369
- // Latitude 90 needs to be adjusted to be just less, so the returned code
370
- // can also be decoded.
371
- if ( latitude == 90 ) {
372
- latitude = latitude - computeLatitudePrecision ( optLength ) ;
373
- }
374
- var code = '' ;
375
366
376
- // Compute the code.
377
- // This approach converts each value to an integer after multiplying it by
378
- // the final precision. This allows us to use only integer operations, so
379
- // avoiding any accumulation of floating point representation errors.
367
+ // This approach converts each value to an integer after multiplying it by the final precision.
368
+ // This allows us to use only integer operations, so avoiding any accumulation of floating
369
+ // point representation errors.
370
+
371
+ // Convert latitude into a positive integer clipped into the range 0-(just under 180*2.5e7).
372
+ // Latitude 90 needs to be adjusted to be just less, so the returned code can also be decoded.
373
+ var latVal = Math . round ( latitude * FINAL_LAT_PRECISION ) ;
374
+ latVal += LATITUDE_MAX * FINAL_LAT_PRECISION ;
375
+ if ( latVal < 0 ) {
376
+ latVal = 0 ;
377
+ } else if ( latVal >= 2 * LATITUDE_MAX * FINAL_LAT_PRECISION ) {
378
+ latVal = 2 * LATITUDE_MAX * FINAL_LAT_PRECISION - 1 ;
379
+ }
380
+ // Convert longitude into a positive integer and normalise it into the range 0-360*8.192e6.
381
+ var lngVal = Math . round ( longitude * FINAL_LNG_PRECISION ) ;
382
+ lngVal += LONGITUDE_MAX * FINAL_LNG_PRECISION ;
383
+ if ( lngVal < 0 ) {
384
+ lngVal =
385
+ ( lngVal % ( 2 * LONGITUDE_MAX * FINAL_LNG_PRECISION ) ) +
386
+ 2 * LONGITUDE_MAX * FINAL_LNG_PRECISION ;
387
+ } else if ( lngVal >= 2 * LONGITUDE_MAX * FINAL_LNG_PRECISION ) {
388
+ lngVal = lngVal % ( 2 * LONGITUDE_MAX * FINAL_LNG_PRECISION ) ;
389
+ }
380
390
381
- // Multiply values by their precision and convert to positive.
382
- // Force to integers so the division operations will have integer results.
383
- // Note: JavaScript requires rounding before truncating to ensure precision!
384
- var latVal =
385
- Math . floor ( Math . round ( ( latitude + LATITUDE_MAX ) * FINAL_LAT_PRECISION * 1e6 ) / 1e6 ) ;
386
- var lngVal =
387
- Math . floor ( Math . round ( ( longitude + LONGITUDE_MAX ) * FINAL_LNG_PRECISION * 1e6 ) / 1e6 ) ;
391
+ var code = '' ;
388
392
389
393
// Compute the grid part of the code if necessary.
390
394
if ( optLength > PAIR_CODE_LENGTH ) {
@@ -639,22 +643,6 @@ function clipLatitude(latitude) {
639
643
return Math . min ( 90 , Math . max ( - 90 , latitude ) ) ;
640
644
}
641
645
642
- /**
643
- Compute the latitude precision value for a given code length. Lengths <=
644
- 10 have the same precision for latitude and longitude, but lengths > 10
645
- have different precisions due to the grid method having fewer columns than
646
- rows.
647
- @param {number } codeLength A number representing the length (digits) in a
648
- code.
649
- @return {number } The height of the code area in degrees latitude.
650
- */
651
- function computeLatitudePrecision ( codeLength ) {
652
- if ( codeLength <= 10 ) {
653
- return Math . pow ( 20 , Math . floor ( codeLength / - 2 + 2 ) ) ;
654
- }
655
- return Math . pow ( 20 , - 3 ) / Math . pow ( GRID_ROWS , codeLength - 10 ) ;
656
- }
657
-
658
646
/**
659
647
Normalize a longitude into the range -180 to 180, not including 180.
660
648
0 commit comments