A swift library to use What3words autosuggest
The what3words Swift autosuggest is a simple UITextfield extension, which gives you access to,
- All properties of UITextfield
- Dropdown autosuggest search results
- Custom
IBInspectable
properties like Border Color, Width etc,. - Custom
W3wSuggestion
properties like Country Clipping, number of results etc,.
This repository contains an Xcode project that builds a framework, and test autosuggest component. You may instead choose to skip the framework and simply drag and drop the W3wGeocoder.swift
& W3wtextfield.swift
file into your project.
You can use CocoaPods to install w3w-swift-wrapper
by adding it to your Podfile
:
platform :ios, '10.0'
use_frameworks!
target 'MyApp' do
pod 'W3wSuggestionField', :git => 'https://github.com/what3words/w3w-autosuggest-textfield-swift.git'
end
You can manually drag W3wGeocoder.swift & W3wtextfield.swift
into the project tree. You can then skip the import statement in your code.
Fire up Xcode and create a new Single View App
project with swift
as a language. Xcode will create your new Swift project
If you used the framework via a package manager then use the following:
import W3wSuggestionField
Note: If you skipped the framework and manually dragged and dropped the W3wGeocoder.swift & W3wtextfield.swift
then you don't need use the import statement.
Select ViewController.swift
and add the following code to the class:
@IBOutlet weak var suggestionField : W3wTextField?
Now, open Main.storyboard and drag a UITextfield
to the screen from the Object library. In the Identity Inspector change the custom class to W3wTextField
. The last thing to do is to connect the action to the button. Click the yellow View Controller icon in the View Controller scene.
From the Connections Inspector (last tab on the right sidebar), click and drag the open circle next to suggestionField
to newly created UITextfield
in the storyboard.
Initialize the API by placing the following code into the -viewDidLoad
method
suggestionField!.setAPIKey(APIKey: "<Secret API Key>")
If you run our Enterprise Suite API Server yourself, you may specifty the URL to your own server like so:
suggestionField!.setAPIKey(APIKey: "<Secret API Key>", apiUrl: "https://api.yourserver.com")
To use this library you’ll need a what3words API key, which can be signed up for here.
To get selected three word address from dropdown menu,
suggestionField?.didSelect{(selectedText) in
print("\(selectedText)")
}
There are a range of W3wSuggestion
properties you can use to configure the autosuggest component.
Variable | Example | Description |
---|---|---|
n-results Int |
3 | number of results to return |
autosuggest-focus String |
51.521251,-0.203586 | comma separated lat/lng of point to focus on |
n-focus-results Int |
2 | the number of results within what is returned to apply the focus to |
clip-to-country String |
GB,US | confine results to a given country or comma separated list of countries |
clip-to-bounding-box String |
51.521,-0.343,52.6,2.3324 | Confine results to a bounding box specified using co-ordinates |
clip-to-circle String |
51.521,-0.343,142 | Restrict autosuggest results to a circle, specified by lat,lng,kilometres, where kilometres in the radius of the circle. For convenience, longitude is allowed to wrap around 180 degrees. For example 181 is equivalent to -179. |
clip-to-polygon String |
51.521,-0.343,52.6,2.3324,54.234,8.343,51.521,-0.343 | Restrict autosuggest results to a polygon, specified by a comma-separated list of lat,lng pairs. The polygon should be closed, i.e. the first element should be repeated as the last element; also the list should contain at least 4 entries. The API is currently limited to accepting up to 25 pairs. |
debug Bool |
false | output information to console for debugging or throw error |
Set the default address appearing in the input box Code Example