@@ -38,78 +38,72 @@ import CoreLocation
38
38
///
39
39
/// :see: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
40
40
public struct Polyline {
41
-
42
- /// The array of coordinates
43
- public let coordinates : [ CLLocationCoordinate2D ]
44
- /// The encoded polyline
45
- public let encodedPolyline : String = " "
46
-
47
- /// The array of levels
48
- public let levels : [ UInt32 ] ?
49
- /// The encoded levels
50
- public let encodedLevels : String = " "
51
-
52
- /// The array of location (computed from coordinates)
53
- public var locations : [ CLLocation ] {
54
- return toLocations ( coordinates)
55
- }
56
-
41
+
42
+ /// The array of coordinates (nil if polyline cannot be decoded)
43
+ public let coordinates : [ CLLocationCoordinate2D ] ?
44
+ /// The encoded polyline
45
+ public let encodedPolyline : String
46
+
47
+ /// The array of levels (nil if cannot be decoded, or is not provided)
48
+ public let levels : [ UInt32 ] ?
49
+ /// The encoded levels (nil if cannot be encoded, or is not provided)
50
+ public let encodedLevels : String ?
51
+
52
+ /// The array of location (computed from coordinates)
53
+ public var locations : [ CLLocation ] ? {
54
+ return self . coordinates. map ( toLocations )
55
+ }
56
+
57
57
// MARK: - Public Methods -
58
58
59
- /// This designated init encodes an [CLLocationCoordinate2D] to a String
60
- ///
61
- /// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
62
- /// :param: levels The optional array of levels that you want to encode
59
+ /// This designated initializer encodes a [CLLocationCoordinate2D]
60
+ ///
61
+ /// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
62
+ /// :param: levels The optional array of levels that you want to encode (default: nil)
63
+ /// :param: precision The precision used for encoding (default: 1e5)
63
64
public init ( coordinates: [ CLLocationCoordinate2D ] , levels: [ UInt32 ] ? = nil , precision: Double = 1e5 ) {
64
-
65
- self . coordinates = coordinates
66
- self . levels = levels
67
-
68
- encodedPolyline = encodeCoordinates ( coordinates, precision: precision)
69
-
70
- if let levelsToEncode = levels {
71
- encodedLevels = encodeLevels ( levelsToEncode)
72
- }
73
- }
74
-
75
- /// This designated init decodes a polyline String to an [CLLocation]
76
- ///
77
- /// :param: encodedPolyline The polyline that you want to decode
78
- /// :param: encodedLevels The levels that you want to decode
79
- public init ( encodedPolyline: String , encodedLevels: String ? = nil , precision: Double = 1e5 ) {
80
-
81
- self . encodedPolyline = encodedPolyline
82
- coordinates = [ ]
83
-
84
- if let decodedCoordinates: [ CLLocationCoordinate2D ] = decodePolyline ( encodedPolyline, precision: precision) {
85
- coordinates = decodedCoordinates
86
- }
87
-
88
- if let levelsToDecode = encodedLevels {
89
- self . encodedLevels = levelsToDecode
90
-
91
- if let decodedLevels = decodeLevels ( levelsToDecode) {
92
- levels = decodedLevels
93
- }
94
- }
95
- }
96
-
97
- /// This init encodes an [CLLocation] to a String
98
- ///
99
- /// :param: locations The array of CLLocation that you want to encode
100
- /// :param: levels The optional array of levels that you want to encode
101
- public init ( locations: [ CLLocation ] , levels: [ UInt32 ] ? = nil , precision: Double = 1e5 ) {
102
-
103
- let coordinates = toCoordinates ( locations)
65
+
66
+ self . coordinates = coordinates
67
+ self . levels = levels
68
+
69
+ encodedPolyline = encodeCoordinates ( coordinates, precision: precision)
70
+
71
+ encodedLevels = levels. map ( encodeLevels)
72
+ }
73
+
74
+ /// This designated initializer decodes a polyline String
75
+ ///
76
+ /// :param: encodedPolyline The polyline that you want to decode
77
+ /// :param: encodedLevels The levels that you want to decode (default: nil)
78
+ /// :param: precision The precision used for decoding (default: 1e5)
79
+ public init ( encodedPolyline: String , encodedLevels: String ? = nil , precision: Double = 1e5 ) {
80
+
81
+ self . encodedPolyline = encodedPolyline
82
+ self . encodedLevels = encodedLevels
83
+
84
+ coordinates = decodePolyline ( encodedPolyline, precision: precision)
85
+
86
+ levels = self . encodedLevels. flatMap ( decodeLevels)
87
+ }
88
+
89
+ /// This init encodes a [CLLocation]
90
+ ///
91
+ /// :param: locations The array of CLLocation that you want to encode
92
+ /// :param: levels The optional array of levels that you want to encode (default: nil)
93
+ /// :param: precision The precision used for encoding (default: 1e5)
94
+ public init ( locations: [ CLLocation ] , levels: [ UInt32 ] ? = nil , precision: Double = 1e5 ) {
95
+
96
+ let coordinates = toCoordinates ( locations)
104
97
self . init ( coordinates: coordinates, levels: levels, precision: precision)
105
- }
98
+ }
106
99
}
107
100
108
101
// MARK: - Public Functions -
109
102
110
103
/// This function encodes an [CLLocationCoordinate2D] to a String
111
104
///
112
105
/// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
106
+ /// :param: precision The precision used to encode coordinates (default: 1e5)
113
107
///
114
108
/// :returns: A String representing the encoded Polyline
115
109
public func encodeCoordinates( coordinates: [ CLLocationCoordinate2D ] , precision: Double = 1e5 ) -> String {
@@ -131,9 +125,10 @@ public func encodeCoordinates(coordinates: [CLLocationCoordinate2D], precision:
131
125
return encodedPolyline
132
126
}
133
127
134
- /// This function encodes an [CLLocationCoordinate2D ] to a String
128
+ /// This function encodes an [CLLocation ] to a String
135
129
///
136
- /// :param: coordinates The array of CLLocationCoordinate2D that you want to encode
130
+ /// :param: coordinates The array of CLLocation that you want to encode
131
+ /// :param: precision The precision used to encode locations (default: 1e5)
137
132
///
138
133
/// :returns: A String representing the encoded Polyline
139
134
public func encodeLocations( locations: [ CLLocation ] , precision: Double = 1e5 ) -> String {
@@ -152,9 +147,10 @@ public func encodeLevels(levels: [UInt32]) -> String {
152
147
}
153
148
}
154
149
155
- /// This function decodes a String to an [CLLocationCoordinate2D]
150
+ /// This function decodes a String to a [CLLocationCoordinate2D]?
156
151
///
157
152
/// :param: encodedPolyline String representing the encoded Polyline
153
+ /// :param: precision The precision used to decode coordinates (default: 1e5)
158
154
///
159
155
/// :returns: A [CLLocationCoordinate2D] representing the decoded polyline if valid, nil otherwise
160
156
public func decodePolyline( encodedPolyline: String , precision: Double = 1e5 ) -> [ CLLocationCoordinate2D ] ? {
@@ -186,9 +182,10 @@ public func decodePolyline(encodedPolyline: String, precision: Double = 1e5) ->
186
182
return decodedCoordinates
187
183
}
188
184
189
- /// This function decodes a String to an [CLLocation]
185
+ /// This function decodes a String to a [CLLocation]?
190
186
///
191
187
/// :param: encodedPolyline String representing the encoded Polyline
188
+ /// :param: precision The precision used to decode locations (default: 1e5)
192
189
///
193
190
/// :returns: A [CLLocation] representing the decoded polyline if valid, nil otherwise
194
191
public func decodePolyline( encodedPolyline: String , precision: Double = 1e5 ) -> [ CLLocation ] ? {
@@ -205,7 +202,7 @@ public func decodeLevels(encodedLevels: String) -> [UInt32]? {
205
202
var remainingLevels = encodedLevels. unicodeScalars
206
203
var decodedLevels = [ UInt32] ( )
207
204
208
- while countElements ( remainingLevels) > 0 {
205
+ while count ( remainingLevels) > 0 {
209
206
var result = extractNextChunk ( & remainingLevels)
210
207
if result. failed {
211
208
return nil
@@ -275,7 +272,7 @@ private func encodeFiveBitComponents(value: Int) -> String {
275
272
276
273
// MARK: Decode Coordinate
277
274
278
- // We use a byte array (UnsafePointer<Int8>) here for performance reasons. Check with swift 1.2 if we can
275
+ // We use a byte array (UnsafePointer<Int8>) here for performance reasons. Check with swift 1.2 if we can
279
276
// go back to using [Int8]
280
277
private func decodeSingleCoordinate( #byteArray: UnsafePointer< Int8> , #length: Int, inout #position: Int, precision: Double = 1e5 ) -> Result< Double> {
281
278
0 commit comments