search
LoginSignup
3

More than 3 years have passed since last update.

posted at

what3wordsをiOSでいじってみる

はじめに

前回は,what3words APIを使ってみるを書きました.

iOSからwhat3wordsを使うには, w3w-swift-wrapperを使用します.
このREST APIを使うことで, 前回の記事をと同様のこと(forward geocoding, reverse geocoding)をおこなうことができます.

APIKEY取得

前回の記事の方法で取得できますので, そちらで取得しておいてください.

インストール

  • CococaPods(iOS 8+, OS X 10.9+)
Podfile.
platform :ios, '8.0'
use_frameworks!

target 'MyApp' do
    pod 'what3words', :git => 'https://github.com/what3words/w3w-swift-wrapper.git'
end
  • Carthage (iOS 8+, OS X 10.10+)
Carthage.
github "what3words/w3w-swift-wrapper"
  • Swift Package Manager
Package.swift
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/what3words/w3w-swift-wrapper.git", versions: Version(1,0,0)..<Version(1, .max, .max)),
    ]
)
  • Manually (iOS 7+, OS X 10.10+)

W3wGeocoder.swiftを自身のプロジェクトにドラッグし, 追加しましょう.

サンプルコード

AppDelegate.swift
//import文を追加
import what3words 

//func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {}の中に追加
//自身のAPIKEYに置き換える
W3wGeocoder.setup(with: "<Secret API Key>")

Forward Geocode

W3wGeocoder.shared.forwardGeocode(addr: "index.home.raft") { (result, error) in
    print(result)
}

Reverse Geocode

let coords = CLLocationCoordinate2D(latitude: 51.4243877, longitude: -0.34745)
W3wGeocoder.shared.reverseGeocode(coords: coords) { (result, error) in
    print(result)
}

AutoSuggest

W3wGeocoder.shared.autosuggest(addr: "index.home.raft") { (result, error) in
    print(result)
}

StandardBlend

W3wGeocoder.shared.standardBlend(addr: "plan.clips.a", completion: { (result, error) in
    print(result)
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
3