W3W Objective-C Wrapper

An Objective-C library to use the what3words REST API.

This what3words Objective-C wrapper gives you programmatic access to the What3Words 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).

Step 1: Obtain an API Key

Sign up to obtain your free API key.

Step 2: Installation

You can install the What3Words API V3 Objective-C Wrapper into your project using CocoaPods, or Carthage (see the package manager code to the right of this page).

                        
platform :ios, '9.0'
use_frameworks!

target 'MyApp' do
    pod 'what3words', :git => 'https://github.com/what3words/w3w-objectivec-wrapper.git'
end
                    
                        
github "what3words/w3w-objectivec-wrapper"
                    
                        
Drop the W3wGeocoder.m and W3wGeocoder.h file into your project.
                    

Step 3: Setup

Use these header files:

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

Or, if you are using Carthage:

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

First you create a W3wGeocoder object:

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

Step 4: Usage

Convert to 3 word address

Convert coordinates, expressed as latitude and longitude, to a 3 word address. It takes the latitude and longitude as a CLLocationCoordinate2D 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).
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(51.520847, -0.195521);
  
[api convertTo3wa:coordinates completion:^(W3wPlace *place, W3wError *error)
    {
    NSLog(@"%@", place.words);
    }];

Convert to Coordinates

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'

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
[api convertToCoordinates:@"filled.count.soap" format:JSON completion:^(W3wPlace *place, W3wError *error)
    {
    NSLog(@"Coordinates are: (%f, %f)", place.coordinates.latitude, place.coordinates.longitude);
    }];

Auto Suggest

When presented with a 3 words 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 info about autosuggest, including returned results is available in the what3words REST API what3words REST API documentation.

Example: Simple basic call.
[api autosuggest:@"geschaft.planter.carciofi" completion:^(NSArray *suggestions, W3wError *error)
    {
    W3wSuggestion *first_match = suggestions[0];
    NSLog(@"%@", first_match.words);
    }];
Example: Limit the search to words in 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);
   }];
Example: Focus on (51.4243877,-0.34745) and call 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);
    }];

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 info about gridSection, including returned results is available in the what3words REST API what3words REST API documentation.

Example: 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);
    }];

Available Languages

The What3Words system is available in a number of languages. More languages are added all the time, and you ask the API for the current list by calling availableLanguages. More info about availableLanguages, including returned results is available in the what3words REST API what3words REST API documentation.

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

Handling Errors

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

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

Step 5: Putting It All Together

Complete Autosuggest Example:

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

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

  AutoSuggestOption *france = [AutoSuggestOption clipToCountry:@"FR"];
  AutoSuggestOption *place  = [AutoSuggestOption focus:CLLocationCoordinate2DMake(48.856618, 2.3522411)];
  AutoSuggestOption *count  = [AutoSuggestOption numberResults:10];

  [api autosuggest:@"hangry.selfie.x" parameters:@[france, place, count] completion:^(NSArray *suggestions, W3wError *error)
    {
    if (error)
        NSLog(@"There was an %@ error: %@", error.code, error.message);
    else
      {
      NSLog(@"Suggestions Returned:");
      for (W3wSuggestion *suggestion in suggestions)
        {
        NSLog(@"%@ is near %@", suggestion.words, suggestion.nearestPlace);
        }
      }
      
    }];
  
    

Step 6: Further Reading

See the what3words GitHub repository for access to full source code of the this wrapper and further documentation.

Examples of each endpoint are available in the Xcode unit tests provided in the package.