Skip to content

Commit e9e037c

Browse files
authored
Merge pull request #139 from Automatic/objc-batch
Expose the batch geocoding function to Objective-C
2 parents 37567bf + 543f2b6 commit e9e037c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

MapboxGeocoder/MBGeocoder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ open class Geocoder: NSObject {
207207
- returns: The data task used to perform the HTTP request. If, while waiting for the completion handler to execute, you no longer want the resulting placemarks, cancel this task.
208208
*/
209209
@discardableResult
210-
open func batchGeocode<T: GeocodeOptions>(_ options: T, completionHandler: @escaping BatchCompletionHandler) -> URLSessionDataTask where T: BatchGeocodeOptions {
210+
@objc(batchGeocodeWithOptions:completionHandler:)
211+
open func batchGeocode(_ options: GeocodeOptions & BatchGeocodeOptions, completionHandler: @escaping BatchCompletionHandler) -> URLSessionDataTask {
211212
let url = urlForGeocoding(options)
212213

213214
let task = dataTaskWithURL(url, completionHandler: { (data) in

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,30 @@ let task = geocoder.batchGeocode(options) { (placemarksByQuery, attributionsByQu
224224
}
225225
```
226226

227+
```objc
228+
// main.m
229+
MBForwardBatchGeocodeOptions *options = [[MBForwardBatchGeocodeOptions alloc] initWithQueries:@[@"skyline chili", @"gold star chili"]];
230+
options.focalLocation = locationManager.location;
231+
options.allowedScopes = MBPlacemarkScopePointOfInterest;
232+
233+
NSURLSessionDataTask *task = [geocoder batchGeocodeWithOptions:options
234+
completionHandler:^(NSArray<NSArray<MBGeocodedPlacemark *> *> * _Nullable placemarksByQuery,
235+
NSArray<NSString *> * _Nullable attributionsByQuery,
236+
NSError * _Nullable error) {
237+
if (!placemarksByQuery) {
238+
return;
239+
}
240+
241+
MBPlacemark *nearestSkyline = placemarksByQuery[0][0].location;
242+
CLLocationDistance distanceToSkyline = [nearestSkyline distanceFromLocation:locationManager.location];
243+
MBPlacemark *nearestGoldStar = placemarksByQuery[1][0].location;
244+
CLLocationDistance distanceToGoldStar = [nearestGoldStar distanceFromLocation:locationManager.location];
245+
246+
NSString *distance = [NSLengthFormatter stringFromMeters:MIN(distanceToSkyline, distanceToGoldStar)];
247+
NSLog(@"Found a chili parlor %@ away.", distance);
248+
}];
249+
```
250+
227251
Batch geocoding is available to Mapbox enterprise accounts. See the [Mapbox Geocoding](https://www.mapbox.com/geocoding/) website for more information.
228252
229253
## Tests

0 commit comments

Comments
 (0)