@@ -82,9 +82,12 @@ let kMinTrimmableCodeLen = 6
82
82
/// complete valid charset for Open Location Codes.
83
83
let kLegalCharacters = CharacterSet . init ( charactersIn: " 23456789CFGHJMPQRVWX+0 " )
84
84
85
- /// Default size of encoded Open Location Codes.
85
+ /// Default length of encoded Open Location Codes (not including + symbol) .
86
86
public let kDefaultFullCodeLength = 11
87
87
88
+ /// The maximum significant digits in a plus code.
89
+ let kMaxFullCodeLength = 15
90
+
88
91
/// Default truncation amount for short codes.
89
92
public let kDefaultShortCodeTruncation = 4
90
93
@@ -93,7 +96,6 @@ public let kDefaultShortCodeTruncation = 4
93
96
/// chars saved doesn't make up for need to resolve).
94
97
let kMinShortCodeTruncation = 4
95
98
96
-
97
99
/// Coordinates of a decoded Open Location Code.
98
100
/// The coordinates include the latitude and longitude of the lower left and
99
101
/// upper right corners and the center of the bounding box for the area the
@@ -499,11 +501,11 @@ public class OpenLocationCode {
499
501
/// normalised to the range -180 to 180.
500
502
/// - Parameter codeLength: The number of significant digits in the output
501
503
/// code, not including any separator characters. Possible values are;
502
- /// `2`, `4`, `6`, `8`, `10`, `11`, `12`, `13` and above. Lower values
504
+ /// `2`, `4`, `6`, `8`, `10`, `11`, `12`, `13`, `14`, and `15`. Values
505
+ /// above `15` are accepted, but treated as `15`. Lower values
503
506
/// indicate a larger area, higher values a more precise area.
504
507
/// You can also shorten a code after encoding for codes used with a
505
508
/// reference point (e.g. your current location, a city, etc).
506
- ///
507
509
/// - Returns: Open Location Code for the given coordinate.
508
510
public static func encode( latitude: Double ,
509
511
longitude: Double ,
@@ -514,6 +516,8 @@ public class OpenLocationCode {
514
516
// 'Invalid Open Location Code length - '
515
517
return nil
516
518
}
519
+ var codeLength = codeLength
520
+ codeLength = min ( codeLength, kMaxFullCodeLength)
517
521
518
522
// Ensure that latitude and longitude are valid.
519
523
var latitude = clipLatitude ( latitude)
@@ -632,6 +636,10 @@ public class OpenLocationCode {
632
636
code = String ( code. filter {
633
637
kCodeAlphabetCharset. contains ( String ( $0) . unicodeScalars. first!)
634
638
} )
639
+ // Constrain to max digits (NB. plus symbol already removed).
640
+ if ( code. length > kMaxFullCodeLength) {
641
+ code = code. substring ( to: kMaxFullCodeLength)
642
+ }
635
643
636
644
// Decode the lat/lng pair component.
637
645
let codeArea = decodePairs ( code [ 0 ..< kPairCodeLength] ) !
0 commit comments