質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1313閲覧

Mapkitのルート検索にてデータを取得する方法

Sino0000-1

総合スコア0

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/07/13 14:38

編集2021/09/25 18:33

前提・実現したいこと

現在、swiftで地図アプリを作成しています。
作成している地図アプリの中で、ルート検索を行いたいと考え、ルート検索を行えるようにしました。
次にルート検索を行ったデータ(地図上に表示されるルート検索の線が書かれている部分の緯度経度などのデータ)を取得したいと考えています。
どのようにすれば経路順序のデータを取得することができるでしょうか。
最終的には緯度経度のデータをプリントするか、ファイル出力できるようにしたいです。

該当のソースコード

swift

1import UIKit 2import MapKit 3 4class customPin: NSObject, MKAnnotation { 5 var coordinate: CLLocationCoordinate2D 6 var title: String? 7 var subtitle: String? 8 9 init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) { 10 self.title = pinTitle 11 self.subtitle = pinSubTitle 12 self.coordinate = location 13 } 14} 15 16class ViewController: UIViewController, MKMapViewDelegate { 17 18 @IBOutlet weak var mapView: MKMapView! 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 // Do any additional setup after loading the view, typically from a nib. 22 //始点と終点を設定する 23 let sourceLocation = CLLocationCoordinate2D(latitude:35.0702617 , longitude: 135.7584548) 24 let destinationLocation = CLLocationCoordinate2D(latitude:35.000046, longitude: 135.768391) 25 26 27 // add annotation 28 //ピンを設定する 29 let sourcePin = customPin(pinTitle: "京都産業大学", pinSubTitle: "", location: sourceLocation) 30 let destinationPin = customPin(pinTitle: "京都駅", pinSubTitle: "", location: destinationLocation) 31 self.mapView.addAnnotation(sourcePin) 32 self.mapView.addAnnotation(destinationPin) 33 34 35 //経路探索するための情報 36 let sourcePlaceMark = MKPlacemark(coordinate: sourceLocation) 37 let destinationPlaceMark = MKPlacemark(coordinate: destinationLocation) 38 39 //経路探索の情報を与える 40 let directionRequest = MKDirections.Request() 41 directionRequest.source = MKMapItem(placemark: sourcePlaceMark) 42 directionRequest.destination = MKMapItem(placemark: destinationPlaceMark) 43 directionRequest.transportType = .automobile 44 45 let directions = MKDirections(request: directionRequest) 46 directions.calculate { (response, error) in 47 guard let directionResonse = response else { 48 if let error = error { 49 print("we have error getting directions==(error.localizedDescription)") 50 } 51 return 52 } 53 54 print("*************") 55 //経路探索結果をマップに追加する 56 let route = directionResonse.routes[0] 57 58 self.mapView.addOverlay(route.polyline, level: .aboveRoads) 59 print(route.polyline) 60 //縮尺を設定する 61 let rect = route.polyline.boundingMapRect 62 self.mapView.setRegion(MKCoordinateRegion(rect), animated: true) 63 } 64 65 //mapViewのデリゲートを自分にする 66 self.mapView.delegate = self 67 68 69 } 70 71 //MARK:- MapKit delegates 72 //ルートを描画する(線の色や太さを設定する) 73 func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 74 let renderer = MKPolylineRenderer(overlay: overlay) 75 renderer.strokeColor = UIColor.blue 76 renderer.lineWidth = 4.0 77 return renderer 78 } 79 80 override func didReceiveMemoryWarning() { 81 super.didReceiveMemoryWarning() 82 // Dispose of any resources that can be recreated. 83 } 84 85 86}

試したこと

web上で調べたところ、
response!.routes[0].polyline.points() => 緯度と経度の配列
を用いることで経路の緯度と経度を取得することができることがわかりました。
しかし、どのように扱えばいいのかわからない現状です。

補足情報(FW/ツールのバージョンなど)

Xcode12.4

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

MKRoute.Stepを用いることで解決した。

投稿2021/09/29 07:52

Sino0000-1

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問