LoginSignup
1
4

More than 5 years have passed since last update.

iOS Swift Google map SDK for iOS  経路検索

Posted at

Google map SDK for iOS で経路検索

Overview

Google map SDK for iOSを用いた経路検索の際に必要となる、Google が提供しているAPIを用いて、A地点からB地点までの経路を地図上に表示させます。

Google map Direction API

Swift で書かれた経路検索ライブラリー

keisukeYamagishi/Direction

このライブラリーを使って経路検索を試みてみます。

Use it

Via SSH:
SSHを使用しての取得方法

$ git clone git@github.com:keisukeYamagishi/Direction.git

Via https:

Http SSLを用いての取得方法

$ git clone https://github.com/keisukeYamagishi/Direction.git

上記の方法でソースコードを取得してくると実際に動作するサンプルコードをみながら使えますので、非常に便利です。

簡単に始められます。

default map line width 0.6f
default map color blue

推奨される経路の線の色

A library that can convert recommended hexadecimal numbers to colors

SHColor

get route!!!!

Cocoapodsを用いての取得方法

CocoaPods Direction

上記リンク先にcocoapods での使用方法が記載されております。

cocoapods が「ない場合はgemにて取得することができます。

下記を参考にしてください。

$ gem install cocoapods
$ vi ./Podfile

If you do not have the google map SDK for iOS

target 'Target Name' do
  pod 'Direction'
  # google map SDK for iOS
  pod 'GoogleMaps'
  pod 'GooglePlaces'
end

Then, run the following command:

$ pod setup
$ pod install

引数

params result type
from 経路検索開始位置の緯度経度 String or CLLocationCoordinate2D
to 経路検索終了位置の緯度経度 String or CLLocationCoordinate2D
alternative 複数の経路を取得したい場合はこの引数をtrueにします。 Bool default false
mode 経路検索の種類 DirectionType walking driving bicycling

Sample Code

何も指定しない方法、取得できる経路は一つです。

let direction = Direction(from:"35.6775602107869,139.692658446729",to: "35.707848364433,139.701456092298",mode: .walking)
direction.directionCompletion(handler: { (route) in

    for route in route.routes {
        self.mapView.addDirection(path: (route?.overview_polyline?.points)!)
    }

}) { (error) in
    print (error)
}

alternative がtrueになっているので、複数の候補が表示されます。

let direction = Direction(from: "35.6775602107869,139.692658446729", to: "35.707848364433,139.701456092298", alternative: true, mode: .walking)

direction.directionCompletion(handler: { (route) in
    for route in route.routes {
        self.mapView.addDirection(path: (route?.overview_polyline?.points)!)
  }
}) { (error) in
    print (error)
}
1
4
0

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
  3. You can use dark theme
What you can do with signing up
1
4