This is a tutorial for using what3words Swift API wrapper.
To use this library you’ll need a what3words API key, which can be signed up for here. If you wish to use the Voice API calls then you must add a Voice API plan to your account.
Add the Swift Package at https://github.com/what3words/w3w-swift-wrapper to your project:
https://github.com/what3words/w3w-swift-wrapper.git
- From Xcode's
File
menu chooseSwift Packages
thenAdd Package Dependancy
. - The
Choose Package Repository
window appears. Addhttps://github.com/what3words/w3w-swift-wrapper.git
in the search box, and click onNext
. - If you are satisfied with the selected version branch choices, click
Next
again. - You should then be shown "Package Product"
W3WSwiftWrapper
. ChooseFinish
.
Xcode should now automatically install w3w-swift-wrapper
In your ViewController add the following import statements to the top of the file:
import W3WSwiftApi
import CoreLocation
Add the following to viewDidLoad()
and be sure to use you API key:
override func viewDidLoad() {
super.viewDidLoad()
// instantiate the API
let api = What3WordsV3(apiKey: "YourApiKey")
// get a three word address from lat,long coordinates
let coords = CLLocationCoordinate2D(latitude: 51.4243877, longitude: -0.34745)
api.convertTo3wa(coordinates: coords, language: "en") { square, error in
if let e = error {
print(e) // print an error if one happens (did you use your API key?)
}
if let words = square?.words {
print("The address for \(coords.latitude),\(coords.longitude) is \(words)")
}
}
// get lat,long coordinates from a three word address
api.convertToCoordinates(words: "filled.count.soap") { square, error in
if let coords = square?.coordinates {
print("The coordinates for filled.count.soap is \(coords.latitude),\(coords.longitude)")
}
}
// get a list of suggestions for a partial three word address
api.autosuggest(text: "filled.count.soa", options: W3WOption.focus(coords)) { (suggestions, error) in
print("Found:")
for suggestion in suggestions ?? [] {
print(" - ", suggestion.words ?? "")
}
}
}
You should see the following output in your console:
The address for 51.4243877,-0.34745 is fled.this.that)
The coordinates for filled.count.soap is 51.520847,-0.195521
Found:
- filled.count.soap
- filled.count.loss
- will.count.soak