Skip to content

Commit 1e04971

Browse files
committed
Updated readme
Migrated examples to Swift 3, correcting some API misusage along the way. Point Podfile and Cartfile to master for now, until the first Swift 3 release.
1 parent 6d00a2f commit 1e04971

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

README.md

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ Download a framework build from [the releases page](https://github.com/mapbox/Ma
2020
In your Podfile:
2121

2222
```podspec
23-
pod 'MapboxGeocoder.swift', :git => 'https://github.com/mapbox/MapboxGeocoder.swift.git', :tag => 'v0.5.2'
23+
pod 'MapboxGeocoder.swift', :git => 'https://github.com/mapbox/MapboxGeocoder.swift.git', :branch => 'master'
2424
```
2525

2626
**[Carthage](https://github.com/Carthage/Carthage)**
2727

2828
In your Cartfile:
2929

3030
```sh
31-
github "Mapbox/MapboxGeocoder.swift" ~> 0.5
31+
github "Mapbox/MapboxGeocoder.swift" "master"
3232
```
3333

34+
v0.5.2 is the last release of MapboxGeocoder.swift written in Swift 2.3. The `swift2.3` branch corresponds to this release, plus any critical bug fixes that have been applied since. All subsequent releases will be based on the `master` branch, which is written in Swift 3. The Swift examples below are written in Swift 3; see the `swift2.3` branch’s readme for Swift 2.3 examples.
35+
3436
This repository includes example applications written in both Swift and Objective-C showing use of the framework (as well as a comparison of writing apps in either language). More examples and detailed documentation are available in the [Mapbox API Documentation](https://www.mapbox.com/api-documentation/?language=Swift#geocoding).
3537

3638
## Usage
@@ -67,7 +69,7 @@ let geocoder = Geocoder.sharedGeocoder
6769
MBGeocoder *geocoder = [MBGeocoder sharedGeocoder];
6870
```
6971

70-
With the geocoder in hand, construct a geocode options object and pass it into the `Geocoder.geocode(options:completionHandler:)` method.
72+
With the geocoder in hand, construct a geocode options object and pass it into the `Geocoder.geocode(_:completionHandler:)` method.
7173

7274
### Forward geocoding
7375

@@ -84,27 +86,29 @@ let options = ForwardGeocodeOptions(query: "200 queen street")
8486
// To refine the search, you can set various properties on the options object.
8587
options.allowedISOCountryCodes = ["CA"]
8688
options.focalLocation = CLLocation(latitude: 45.3, longitude: -66.1)
87-
options.allowedScopes = [.Address, .PointOfInterest]
89+
options.allowedScopes = [.address, .pointOfInterest]
8890

89-
let task = geocoder.geocode(options: options) { (placemarks, attribution, error) in
90-
if let placemark = placemarks?[0] {
91-
print(placemark.name)
92-
// 200 Queen St
93-
print(placemark.qualifiedName)
94-
// 200 Queen St, Saint John, New Brunswick E2L 2X1, Canada
95-
96-
let coordinate = placemark.location!.coordinate
97-
print("\(coordinate.latitude), \(coordinate.longitude)")
98-
// 45.270093, -66.050985
99-
100-
#if !os(tvOS)
91+
let task = geocoder.geocode(options) { (placemarks, attribution, error) in
92+
guard let placemark = placemarks?.first else {
93+
return
94+
}
95+
96+
print(placemark.name)
97+
// 200 Queen St
98+
print(placemark.qualifiedName)
99+
// 200 Queen St, Saint John, New Brunswick E2L 2X1, Canada
100+
101+
let coordinate = placemark.location.coordinate
102+
print("\(coordinate.latitude), \(coordinate.longitude)")
103+
// 45.270093, -66.050985
104+
105+
#if !os(tvOS)
101106
let formatter = CNPostalAddressFormatter()
102-
print(formatter.stringFromPostalAddress(placemark.postalAddress))
107+
print(formatter.string(from: placemark.postalAddress!))
103108
// 200 Queen St
104109
// Saint John New Brunswick E2L 2X1
105110
// Canada
106-
#endif
107-
}
111+
#endif
108112
}
109113
```
110114

@@ -121,10 +125,10 @@ options.allowedISOCountryCodes = @[@"CA"];
121125
options.focalLocation = [[CLLocation alloc] initWithLatitude:45.3 longitude:-66.1];
122126
options.allowedScopes = MBPlacemarkScopeAddress | MBPlacemarkScopePointOfInterest;
123127

124-
NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options
125-
completionHandler:^(NSArray<MBGeocodedPlacemark *> * _Nullable placemarks,
126-
NSString * _Nullable attribution,
127-
NSError * _Nullable error) {
128+
NSURLSessionDataTask *task = [geocoder geocode:options
129+
completionHandler:^(NSArray<MBGeocodedPlacemark *> * _Nullable placemarks,
130+
NSString * _Nullable attribution,
131+
NSError * _Nullable error) {
128132
MBPlacemark *placemark = placemarks[0];
129133
NSLog(@"%@", placemark.name);
130134
// 200 Queen St
@@ -154,17 +158,20 @@ _Reverse geocoding_ takes a geographic coordinate and produces a hierarchy of pl
154158
let options = ReverseGeocodeOptions(coordinate: CLLocationCoordinate2D(latitude: 40.733, longitude: -73.989))
155159
// Or perhaps: ReverseGeocodeOptions(location: locationManager.location)
156160
157-
let task = geocoder.geocode(options: options) { (placemarks, attribution, error) in
158-
let placemark = placemarks[0]
159-
print(placemark.imageName)
161+
let task = geocoder.geocode(options) { (placemarks, attribution, error) in
162+
guard let placemark = placemarks?.first else {
163+
return
164+
}
165+
166+
print(placemark.imageName ?? "")
160167
// telephone
161-
print(placemark.genres?.joinWithSeparator(", "))
168+
print(placemark.genres?.joined(separator: ", ") ?? "")
162169
// computer, electronic
163-
print(placemark.region?.name)
170+
print(placemark.administrativeRegion?.name ?? "")
164171
// New York
165-
print(placemark.region?.code)
172+
print(placemark.administrativeRegion?.code ?? "")
166173
// US-NY
167-
print(placemark.place?.wikidataItemIdentifier)
174+
print(placemark.place?.wikidataItemIdentifier ?? "")
168175
// Q60
169176
}
170177
```
@@ -174,18 +181,18 @@ let task = geocoder.geocode(options: options) { (placemarks, attribution, error)
174181
MBReverseGeocodeOptions *options = [[MBReverseGeocodeOptions alloc] initWithCoordinate: CLLocationCoordinate2DMake(40.733, -73.989)];
175182
// Or perhaps: [[MBReverseGeocodeOptions alloc] initWithLocation:locationManager.location]
176183

177-
NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options
178-
completionHandler:^(NSArray<MBGeocodedPlacemark *> * _Nullable placemarks,
179-
NSString * _Nullable attribution,
180-
NSError * _Nullable error) {
184+
NSURLSessionDataTask *task = [geocoder geocode:options
185+
completionHandler:^(NSArray<MBGeocodedPlacemark *> * _Nullable placemarks,
186+
NSString * _Nullable attribution,
187+
NSError * _Nullable error) {
181188
MBPlacemark *placemark = placemarks[0];
182189
NSLog(@"%@", placemark.imageName);
183190
// telephone
184191
NSLog(@"%@", [placemark.genres componentsJoinedByString:@", "]);
185192
// computer, electronic
186-
NSLog(@"%@", placemark.region.name);
193+
NSLog(@"%@", placemark.administrativeRegion.name);
187194
// New York
188-
NSLog(@"%@", placemark.region.code);
195+
NSLog(@"%@", placemark.administrativeRegion.code);
189196
// US-NY
190197
NSLog(@"%@", placemark.place.wikidataItemIdentifier);
191198
// Q60
@@ -194,7 +201,7 @@ NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options
194201
195202
### Batch geocoding
196203
197-
With _batch geocoding_, you can perform up to 50 distinct forward or reverse geocoding requests simultaneously and store the results in a private database. Create a ForwardBatchGeocodingOptions or ReverseBatchGeocodingOptions object in Swift, or an MBForwardBatchGeocodingOptions or MBReverseBatchGeocodingOptions object in Objective-C, and pass it into the `Geocoder.batchGeocode(options:completionHandler:)` method.
204+
With _batch geocoding_, you can perform up to 50 distinct forward or reverse geocoding requests simultaneously and store the results in a private database. Create a ForwardBatchGeocodingOptions or ReverseBatchGeocodingOptions object in Swift, or an MBForwardBatchGeocodingOptions or MBReverseBatchGeocodingOptions object in Objective-C, and pass it into the `Geocoder.batchGeocode(_:completionHandler:)` method.
198205
199206
Batch geocoding is available to Mapbox enterprise accounts. See the [Mapbox Geocoding](https://www.mapbox.com/geocoding/) website for more information.
200207

0 commit comments

Comments
 (0)