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

Objective-C

intermediate

This what3words Objective-C wrapper gives you programmatic access to the what3Words Public API version 3.

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 Objective-C Wrapper into your project using CocoaPods, or Carthage (see the package manager code below). Find it on GitHub.

platform :ios, '9.0'
use_frameworks!

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

Carthage:

github "what3words/w3w-objectivec-wrapper"
Copied

For manual installation, Drop the W3wGeocoder.m and W3wGeocoder.h file into your project.

3

Setup

Use these header files:

#import "W3wGeocoder.h"
#import <CoreLocation/CoreLocation.h>
Copied

Or, if you are using Carthage:

#import <what3words/what3words.h>
#import <CoreLocation/CoreLocation.h>
Copied

First create a W3wGeocoder object:

W3wGeocoder *api = [[W3wGeocoder alloc] initWithApiKey:@"<Your Secret Key>"];
Copied
4

Usage

Convert to 3 word address

This function convert coordinates (expressed as latitude and longitude) to a 3 word address. It takes the latitude and longitude as a CLLocationCoordinate2D object.

More information 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).

CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(51.520847, -0.195521);
  
[api convertTo3wa:coordinates completion:^(W3wPlace *place, W3wError *error)
    {
    NSLog(@"%@", place.words);
    }];
Copied

Convert to coordinates

This function converts a 3 word address to a position, expressed as coordinates of latitude and longitude.

It takes the words parameter as a string of 3 words 'filled.count.soap'

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

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

[api convertToCoordinates:@"filled.count.soap" format:JSON completion:^(W3wPlace *place, W3wError *error)
    {
    NSLog(@"Coordinates are: (%f, %f)", place.coordinates.latitude, place.coordinates.longitude);
    }];
Copied

AutoSuggest

When presented with a 3 word address which 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:

[api autosuggest:@"geschaft.planter.carciofi" completion:^(NSArray *suggestions, W3wError *error)
    {
    W3wSuggestion *first_match = suggestions[0];
    NSLog(@"%@", first_match.words);
    }];
Copied

Code example AutoSuggest, clipping the results returned to Germany:

[api autosuggest:@"geschaft.planter.carciofi" parameters:@[[AutoSuggestOption clipToCountry:@"DE"], [AutoSuggestOption numberResults:4]] completion:^(NSArray *suggestions, W3wError *error)
   {
   W3wSuggestion *first_match = suggestions[0];
   NSLog(@"%@", first_match.words);
   }];
Copied

Code example Focus on (51.4243877,-0.34745) and ask for 6 suggestions:

[api autosuggest:@"fun.with.code" parameters:@[[AutoSuggestOption focus:CLLocationCoordinate2DMake(51.4243877, -0.3474524)], [AutoSuggestOption numberResults:6]] completion:^(NSArray *suggestions, W3wError *error)
    {
    W3wSuggestion *first_match = suggestions[0];
    NSLog(@"%@", first_match.words);
    }];
Copied

Code example AutoSuggest, with Generic Voice input type.

[api autosuggest:@"fun with code" parameters:@[[AutoSuggestOption fallBackLanguage:@"EN"], [AutoSuggestOption inputType:GENERIC_VOICE]] completion:^(NSArray *suggestions, W3wError *error)
    {
    W3wSuggestion *first_match = suggestions[0];
    NSLog(@"%@", first_match.words);
    }];
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.

[api gridSection:52.208867 west_lng:0.117540 north_lat:52.207988 east_lng:0.116126 format:JSON completion:^(NSArray *grid, W3wError *error)
    {
    W3wLine *first_line = grid[0];
    NSLog(@"First line goes from (%.5f,%.5f) to (%.5f,%.5f)", first_line.start.latitude, first_line.start.longitude, first_line.end.latitude, first_line.end.longitude);
    }];
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.

[api availableLanguages:^(NSArray *languages, W3wError *error)
    {
    W3wLanguage *language = languages[0];
    NSLog(@"First language returned: %@", language.name);
    }];
Copied

Handling errors

All functions call the completion block with error as the second parameter. Be sure to check it for possible problems.

[api convertToCoordinates:@"aaaa.bbbb.cccc" format:JSON completion:^(W3wPlace *result, W3wError *error)
    {
    if (error)
        NSLog(@"There was an %@ error: %@", error.code, error.message);
    }];
Copied

Related tutorials

Back to top