Tutorials | what3words
We use cookies, including third party cookies, to improve your experience and for marketing purposes. By continuing to browse we assume you have consented to their use.Learn more
got it
All tutorials

Swift

intermediate

This what3words Swift wrapper gives you programmatic access to the what3Words public API v3.

The what3words API is a fast, simple interface which allows you to convert 3 word addresses such as ///filled.count.soap to latitude and longitude coordinates such as (-0.203586, 51.521251) and vice versa. It features a powerful AutoSuggest function, which can validate and autocorrect user input and limit it to certain geographic areas (this powers the search box on our map site). It allows you to request a section of the what3words grid (which can be requested as GeoJSON for easy display on online maps), and to request the list of all languages supported by what3words. For advanced users, AutoSuggest can be used to post-process voice output. See links on the left to navigate.

All coordinates are latitude,longitude pairs in standard WGS-84 (as commonly used worldwide in GPS systems). All latitudes must be in the range of -90 to 90 (inclusive).

You will need a what3words API key to complete this tutorial.

2

Installation

You can install the what3words API V3 Swift Wrapper Framework into your project using CocoaPods, Carthage, the Swift Package manager, or import the swift code manually. Find it on GitHub.

CocoPods:

platform :ios, '9.0'
use_frameworks!

target 'MyApp' do
    pod 'what3words', :git => 'https://github.com/what3words/w3w-swift-wrapper.git'
end      
Copied

Carthage:

github "what3words/w3w-swift-wrapper"
Copied

Swift Package Manager:

// include this line in your dependencies:             
.package(url: "https://github.com/what3words/w3w-swift-wrapper.git", .branch("master"))

// include "what3words" in your target dependancies:
dependencies: ["what3words"])
Copied

For manual installation,Drag and drop the W3wGeocoder.swift file into your Xcode project.

3

Setup

If you are using it as a framework, import the module as follows in any swift file you wish to use what3Words:

import what3words
import CoreLocation
Copied

You must call setup with your API key before you do anything else:

W3wGeocoder.setup(with: "<Secret API Key>")
Copied

If you run our Enterprise Suite API Server yourself, you may specify the URL to your own server like so:

W3wGeocoder.setup(with: "<Secret API Key>", apiUrl: "https://api.yourserver.com")
Copied

Additionally, if you run the Enterprise Suite API Server there is another optional setup() parameter: customHeaders. Use this if you need to send custom headers to your own server:

W3wGeocoder.setup(with: "<Secret API Key>", apiUrl: "https://api.yourserver.com", customHeaders: ["x-header-1":"value-1", "x-header-2":"value-2"])
Copied
4

Usage

Convert to 3 word address

Convert a 3 word address to a position, expressed as coordinates of latitude and longitude.

This function takes the words parameter as a string of 3 words 'table.book.chair'

This returns a W3wPlace object. More info about convertTo3wa, including returned results is available in the what3words REST API what3words REST API documentation.

Example: Find the words for (51.520847, -0.195521).

let coords = CLLocationCoordinate2D(latitude: 51.520847, longitude: -0.195521)
W3wGeocoder.shared.convertTo3wa(coordinates: coords) { (place, error) in
    print(place?.words)
}
Copied

Convert to coordinates

To convert coordinates, expressed as latitude and longitude to a 3 word address, call convertToCoordinates. It takes the latitude and longitude as a CLLocationCoordinate2D object.

This returns a W3wPlace object. More info about convertToCoordinates, including returned results is available in the what3words REST API what3words REST API documentation.

Example: Find the cordinates for ///filled.count.soap

W3wGeocoder.shared.convertToCoordinates(words: "filled.count.soap") { (place, error) in
    print(place?.coordinates.latitude, place?.coordinates.longitude)
}
Copied

AutoSuggest

When presented with a 3 words address that may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.

This method provides corrections for mis-typed words (including plural vs singular), and words being in the wrong order.

Optionally, clipping can narrow down the possibilities, and limit results to:

  • One or more countries
  • A geographic area (a circle, box or polygon)

This dramatically improves results, so we recommend that you use clipping if possible.

To improve results even further, set the focus to user’s current location. This will make autosuggest return results which are closer to the user.

More information about autosuggest, including returned results is available in the what3words REST API documentation.

This returns an Array? of W3wSuggestion objects. More info about autosuggest, including returned results is available in the what3words REST API what3words REST API documentation.

Basic call:

Code example Simple basic call:

W3wGeocoder.shared.autosuggest(input: "fun.with.code") { (suggestions, error) in
    print(suggestions)
}
Copied

Code example AutoSuggest, clipping the results returned to Germany:

W3wGeocoder.shared.autosuggest(input: "geschaft.planter.carciofi", options: ClipToCountry(country:"DE")) { (suggestions, error) in
    print(suggestions)
}
Copied

Code example Focus on (51.4243877,-0.34745)

let coords = CLLocationCoordinate2D(latitude: 51.4243877, longitude: -0.34745)
W3wGeocoder.shared.autosuggest(input: "index.raft.ho", options: Focus(focus: coords), NumberResults(numberOfResults: 3) ) { (suggestions, error) in
    print(suggestions)
}
Copied

Code example AutoSuggest, with Generic Voice input type.

W3wGeocoder.shared.autosuggest(input: "fun with code", options: FallbackLanguage(language: "EN"), InputType(inputType: .genericVoice)) { (suggestions, error) in
    print(suggestions)
}
Copied

Grid section

Grid section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.

The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned.

More information about gridSection, including returned results is available in the what3words REST API documentation.

Get a grid for (52.208867,0.117540) in the south-west, and (52.207988,0.116126) in the north-east.

W3wGeocoder.shared.gridSection(south_lat: 52.208867, west_lng: 0.117540, north_lat: 52.207988, east_lng: 0.116126) { (lines, error) in
    print(lines)
}
Copied

Available languages

This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.

More information about availableLanguages, including returned results is available in the what3words REST API documentation.

W3wGeocoder.shared.availableLanguages() { (languages, error) in
    print(languages)
}
Copied

Handling errors

Errors returned from the API can be caught with the wrapper through the use of a catch function.

Within the catch function, code and message values which represent the error, are accessible from the error object parameter

W3wGeocoder.shared.convertToCoordinates(words: "filled.count.soap") { (result, error) in
      if let e = error {
        print(e.code, e.message)
    }
}
Copied

Related tutorials

Back to top