|
| 1 | + |
| 2 | +# GoongGeocoder |
| 3 | + |
| 4 | +GoongGeocoder makes it easy to connect your iOS application to the [Goong Geocoding API](https://docs.goong.io/rest/guide/#geocode) and [Goong Autocomplete API](https://docs.goong.io/rest/guide/#place) |
| 5 | + |
| 6 | + |
| 7 | +## Getting started |
| 8 | + |
| 9 | +Specify the following dependency in your [CocoaPods](http://cocoapods.org/) Podfile: |
| 10 | + |
| 11 | +```podspec |
| 12 | +pod 'GoongGeocoder' |
| 13 | +``` |
| 14 | + |
| 15 | +Then `import GoongGeocoder` or `@import GoongGeocoder;`. |
| 16 | + |
| 17 | +For Objective-C targets, it may be necessary to enable the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting. |
| 18 | + |
| 19 | +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 [Goong API Documentation](https://docs.goong.io). |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +You will need a [Goong API KEY](https://account.goong.io) in order to use the API. If you’re already using the [Goong Maps SDK for iOS](https://docs.goong.io/ios/guide/), GoongGeocoder.swift automatically recognizes your access token, as long as you’ve placed it in the `GoongAccessToken` key of your application’s Info.plist file. |
| 24 | + |
| 25 | +### Basics |
| 26 | + |
| 27 | +The main geocoder class is Geocoder in Swift or GoongGeocoder in Objective-C. Create a geocoder object using your access token: |
| 28 | + |
| 29 | +```swift |
| 30 | +// main.swift |
| 31 | +import GoongGeocoder |
| 32 | + |
| 33 | +let geocoder = Geocoder(accessToken: "<#your access token#>") |
| 34 | +``` |
| 35 | + |
| 36 | +```objc |
| 37 | +// main.m |
| 38 | +@import GoongGeocoder; |
| 39 | + |
| 40 | +GoongGeocoder *geocoder = [[GoongGeocoder alloc] initWithAccessToken:@"<#your access token#>"]; |
| 41 | +``` |
| 42 | +
|
| 43 | +Alternatively, you can place your access token in the `GoongAccessToken` key of your application’s Info.plist file, then use the shared geocoder object: |
| 44 | +
|
| 45 | +```swift |
| 46 | +// main.swift |
| 47 | +let geocoder = Geocoder.shared |
| 48 | +``` |
| 49 | + |
| 50 | +```objc |
| 51 | +// main.m |
| 52 | +GoongGeocoder *geocoder = [GoongGeocoder sharedGeocoder]; |
| 53 | +``` |
| 54 | + |
| 55 | +With the geocoder in hand, construct a geocode options object and pass it into the `Geocoder.geocode(_:completionHandler:)` method. |
| 56 | + |
| 57 | +### Autocomplete or Forward geocoding |
| 58 | + |
| 59 | +_Forward geocoding_ takes a human-readable query, such as a place name or address, and produces any number of geographic coordinates that correspond to that query. To perform forward geocoding, use ForwardGeocodeOptions in Swift or GoongForwardGeocodeOptions in Objective-C. |
| 60 | + |
| 61 | +```swift |
| 62 | +// main.swift |
| 63 | + |
| 64 | +let options = ForwardGeocodeOptions(query: "san bay noi bai") |
| 65 | +options.focalLocation = CLLocation(latitude: 21, longitude: 105) |
| 66 | +let task = geocoder.geocode(options) { (result, error) in |
| 67 | + guard let result = result else { |
| 68 | + return |
| 69 | + } |
| 70 | + print(result.predictions) |
| 71 | + // GeocodeResult provide predictions if you use ForwardGeocodeOptions |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +```objc |
| 76 | +// main.m |
| 77 | + |
| 78 | +GoongForwardGeocodeOptions *options = [[GoongForwardGeocodeOptions alloc] initWithQuery:@"san bay noi bai"]; |
| 79 | +options.focalLocation = [[CLLocation alloc] initWithLatitude:21 longitude:105]; |
| 80 | + |
| 81 | + |
| 82 | +NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options |
| 83 | + completionHandler:^(GeocodeResult * _Nullable result, |
| 84 | + NSError * _Nullable error) { |
| 85 | + // GeocodeResult provide predictions if you use GoongForwardGeocodeOptions |
| 86 | +}]; |
| 87 | +``` |
| 88 | +
|
| 89 | +### Reverse geocoding |
| 90 | +
|
| 91 | +_Reverse geocoding_ takes a geographic coordinate and produces a hierarchy of places, often beginning with an address, that describes the coordinate’s location. To perform reverse geocoding, use ReverseGeocodeOptions in Swift or GoongReverseGeocodeOptions in Objective-C. |
| 92 | +
|
| 93 | +```swift |
| 94 | +// main.swift |
| 95 | +let options = ReverseGeocodeOptions(coordinate: CLLocationCoordinate2D(latitude: 21.21760917728946, longitude: 105.7922871444448)) |
| 96 | +// Or perhaps: ReverseGeocodeOptions(location: locationManager.location) |
| 97 | +
|
| 98 | +let task = geocoder.geocode(options) { (result, error) in |
| 99 | + guard let result = result else { |
| 100 | + return |
| 101 | + } |
| 102 | + // GeocodeResult provide placemarks if you use ReverseGeocodeOptions |
| 103 | + print(result.placemarks.first) |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +```objc |
| 108 | +// main.m |
| 109 | +GoongReverseGeocodeOptions *options = [[GoongReverseGeocodeOptions alloc] initWithCoordinate: CLLocationCoordinate2DMake(21.21760917728946, 105.7922871444448)]; |
| 110 | + |
| 111 | + |
| 112 | +NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options |
| 113 | + completionHandler:^(GeocodeResult * _Nullable result, |
| 114 | + NSError * _Nullable error) { |
| 115 | + |
| 116 | +}]; |
| 117 | +``` |
| 118 | +
|
| 119 | +
|
0 commit comments