@@ -143,7 +143,12 @@ public class AtlasKit {
143
143
" api-key " : datasource. apiKey!
144
144
]
145
145
146
- let request = sessionManager. request ( URL ( string: " https://api.getaddress.io/find/ \( postcode) " ) !,
146
+ guard let searchTerm = postcode. trimmingCharacters ( in: . whitespacesAndNewlines) . addingPercentEncoding ( withAllowedCharacters: . urlPathAllowed) , let url = URL ( string: " https://api.getaddress.io/find/ \( searchTerm) " ) else {
147
+ completion ( nil , . generic)
148
+ return
149
+ }
150
+
151
+ let request = sessionManager. request ( url,
147
152
method: . get,
148
153
parameters: parameters,
149
154
encoding: URLEncoding . methodDependent,
@@ -174,7 +179,7 @@ public class AtlasKit {
174
179
return
175
180
}
176
181
177
- completion ( self ? . formatResults ( data, postcode: postcode. capitalized . removingAllWhitespaces, latitude: latitude, longitude: longitude) , nil )
182
+ completion ( self ? . formatResults ( data, postcode: postcode. uppercased ( ) . removingAllWhitespaces, latitude: latitude, longitude: longitude) , nil )
178
183
}
179
184
}
180
185
}
@@ -184,7 +189,7 @@ public class AtlasKit {
184
189
// MARK: - Utilities
185
190
186
191
private func formatResults( _ items: [ MKPlacemark ] ) -> [ AtlasKitPlace ] {
187
- return items. map ( { AtlasKitPlace ( streetAddress: $0. postalAddress!. street, city: $0. postalAddress!. city, postcode: $0. postalAddress!. postalCode, state: $0. postalAddress!. state, country: $0. postalAddress!. country, location: $0. coordinate) } )
192
+ return items. map ( { AtlasKitPlace ( streetAddress: $0. postalAddress!. street, city: $0. postalAddress!. city, postcode: $0. postalAddress!. postalCode, state: $0. postalAddress!. state, country: $0. postalAddress!. country, location: $0. coordinate) } ) . sorted ( by : { $0 . formattedAddress . localizedStandardCompare ( $1 . formattedAddress ) == . orderedAscending } )
188
193
}
189
194
190
195
@@ -195,17 +200,17 @@ public class AtlasKit {
195
200
let address2 = components. indices. contains ( 1 ) ? components [ 1 ] : " "
196
201
let address3 = components. indices. contains ( 2 ) ? components [ 2 ] : " "
197
202
let address4 = components. indices. contains ( 3 ) ? components [ 3 ] : " "
198
- let address = ( [ address1, address2, address3, address4] . filter ( { !$0. isEmpty } ) . map ( { String ( $0) } ) . joined ( separator: " , " ) )
203
+ let address = ( [ address1, address2, address3, address4] . filter ( { !$0. trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty } ) . map ( { String ( $0) } ) . joined ( separator: " , " ) )
199
204
let city = components. indices. contains ( 5 ) ? components [ 5 ] : " "
200
205
let county = components. indices. contains ( 6 ) ? components [ 6 ] : " "
201
206
202
207
return AtlasKitPlace ( streetAddress: address, city: String ( city) , postcode: postcode, state: String ( county) , country: " United Kingdom " , location: CLLocationCoordinate2D ( latitude: latitude, longitude: longitude) )
203
- } )
208
+ } ) . sorted ( by : { $0 . formattedAddress . localizedStandardCompare ( $1 . formattedAddress ) == . orderedAscending } )
204
209
}
205
210
206
211
207
212
private func formatResults( _ items: [ [ String : Any ] ] ) -> [ AtlasKitPlace ] {
208
- return items. map { item -> AtlasKitPlace ? in
213
+ let addresses = items. map { item -> AtlasKitPlace ? in
209
214
210
215
guard let address = item [ " formatted_address " ] as? String ,
211
216
let geometry = item [ " geometry " ] as? [ String : Any ] ,
@@ -217,6 +222,8 @@ public class AtlasKit {
217
222
218
223
return AtlasKitPlace ( streetAddress: GoogleHelper . extractStreetAddress ( from: address) , city: GoogleHelper . extractCity ( from: address) , postcode: GoogleHelper . extractPostcode ( from: address) , state: GoogleHelper . extractState ( from: address) , country: GoogleHelper . extractCountry ( from: address) , location: CLLocationCoordinate2D ( latitude: latitude, longitude: longitude) )
219
224
} . filter ( { $0 != nil && ( $0!. formattedAddress. first) != nil } ) as! [ AtlasKitPlace ]
225
+
226
+ return addresses. sorted ( by: { $0. formattedAddress. localizedStandardCompare ( $1. formattedAddress) == . orderedAscending } )
220
227
}
221
228
222
229
@@ -235,3 +242,4 @@ extension String {
235
242
}
236
243
237
244
}
245
+
0 commit comments