Skip to content

Commit ce60910

Browse files
committed
Fix batch results
1 parent eacb927 commit ce60910

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

MapboxGeocoder/MBGeocoder.swift

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -209,30 +209,19 @@ open class Geocoder: NSObject {
209209
open func batchGeocode<T: GeocodeOptions>(_ options: T, completionHandler: @escaping BatchCompletionHandler) -> URLSessionDataTask where T: BatchGeocodeOptions {
210210
let url = urlForGeocoding(options)
211211

212-
// TODO: Migrate to Codable
213-
214-
// let task = dataTaskWithURL(url, completionHandler: { (json) in
215-
// let featureCollections = json as! [JSONDictionary]
216-
// let placemarksByQuery = featureCollections.map { (featureCollection) -> [GeocodedPlacemark] in
217-
// assert(featureCollection["type"] as? String == "FeatureCollection")
218-
// let features = featureCollection["features"] as! [JSONDictionary]
219-
// return features.flatMap { GeocodedPlacemark(featureJSON: $0) }
220-
// }
221-
// let attributionsByQuery = featureCollections.map { $0["attribution"] as! String }
222-
// completionHandler(placemarksByQuery, attributionsByQuery, nil)
223-
// }) { (error) in
224-
// completionHandler(nil, nil, error)
225-
// }
226-
227212
let task = dataTaskWithURL(url, completionHandler: { (data) in
228-
// let featureCollections = data as! [JSONDictionary]
229-
// let placemarksByQuery = featureCollections.map { (featureCollection) -> [GeocodedPlacemark] in
230-
// assert(featureCollection["type"] as? String == "FeatureCollection")
231-
// let features = featureCollection["features"] as! [JSONDictionary]
232-
// return features.flatMap { GeocodedPlacemark(featureJSON: $0) }
233-
// }
234-
// let attributionsByQuery = featureCollections.map { $0["attribution"] as! String }
213+
guard let data = data else { return }
214+
let decoder = JSONDecoder()
235215

216+
do {
217+
let result = try decoder.decode([GeocodeResult].self, from: data)
218+
let placemarks = result.map { $0.placemarks }
219+
let attributionsByQuery = result.map { $0.attribution }
220+
completionHandler(placemarks, attributionsByQuery, nil)
221+
222+
} catch {
223+
completionHandler(nil, nil, error as NSError)
224+
}
236225

237226
}) { (error) in
238227
completionHandler(nil, nil, error)

MapboxGeocoder/MBPlacemark.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ open class Placemark: NSObject, Codable {
350350
}
351351
}
352352

353-
struct GeocodeResult: Codable {
353+
internal struct GeocodeResult: Codable {
354354
private enum CodingKeys: String, CodingKey {
355355
case placemarks = "features"
356356
case type
@@ -362,14 +362,19 @@ struct GeocodeResult: Codable {
362362
let placemarks: [GeocodedPlacemark]
363363
}
364364

365-
struct Properties: Codable {
365+
/**
366+
`Properties` represents a concrete subset of the `properties` object
367+
on a GeoJSON feature suited for Geocoding results.
368+
*/
369+
internal struct Properties: Codable {
366370
private enum CodingKeys: String, CodingKey {
367371
case shortCode = "short_code"
368372
case phoneNumber = "tel"
369373
case maki
370374
case address
371375
case category
372376
}
377+
373378
let shortCode: String?
374379
let maki: String?
375380
let phoneNumber: String?

0 commit comments

Comments
 (0)