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

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

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

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

Q&A

0回答

582閲覧

【Swift】MapKitでMKMapItemをセットしてMKDirectionsRequestを生成できない

madotuki

総合スコア8

Swift

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

0グッド

0クリップ

投稿2017/11/16 07:01

###前提・実現したいこと
SwiftとMapKitを用いて現在地から目的地までの経路を表示するアプリを作成しています。
現在地と目的地を代入してあるMKPPlacemarkからMKMapItemを生成したとしたところ、MKMapItemをセットしてMKDirectionsRequestを生成した際、リクエストがnullとなって帰ってきてしまい、経路が描画されません。
詳しくはエラーの出力をご覧ください。

###該当のソースコード

swift

1 func getRoute() 2 { 3 4 // アノテーションの設置 5 let userLocAnnotation: MKPointAnnotation = MKPointAnnotation() 6 userLocAnnotation.coordinate = userLocation 7 myMap.addAnnotation(userLocAnnotation) 8 9 let destLocAnnotation: MKPointAnnotation = MKPointAnnotation() 10 destLocAnnotation.coordinate = destLocation 11 myMap.addAnnotation(destLocAnnotation) 12 13 // 現在地と目的地のMKPlacemarkを生成 14 var fromPlacemark = MKPlacemark(coordinate:userLocation, addressDictionary:nil) 15 var toPlacemark = MKPlacemark(coordinate:destLocation, addressDictionary:nil) 16 17 18 19 // MKPlacemark から MKMapItem を生成 20 var fromItem = MKMapItem(placemark:fromPlacemark) 21 var toItem = MKMapItem(placemark:toPlacemark) 22 print("----------------------") 23 print(fromItem) 24 print(toItem) 25 print("----------------------") 26 // MKMapItem をセットして MKDirectionsRequest を生成 27 let myRequest = MKDirectionsRequest() 28 print(myRequest) 29 print("----------------------") 30 31 32 33 34 myRequest.source = fromItem 35 myRequest.destination = toItem 36 37 myRequest.requestsAlternateRoutes = false 38 myRequest.transportType = MKDirectionsTransportType.walking 39 40 let directions = MKDirections(request: myRequest) 41 directions.calculate(completionHandler: {(response, error) -> Void in 42 if error != nil { 43 return 44 } 45 guard var response = response else { 46 return 47 } 48 if response.routes.isEmpty { 49 return 50 } 51 var route: MKRoute = response.routes[0] as MKRoute 52 // 経路を描画 53 self.myMap.add(route.polyline) 54 55 }) 56 func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 57 let route: MKPolyline = overlay as! MKPolyline 58 let routeRenderer = MKPolylineRenderer(polyline:route) 59 routeRenderer.lineWidth = 5.0 60 routeRenderer.strokeColor = UIColor.red 61 return routeRenderer 62 } 63 } 64

###エラー出力

swift

12017-11-16 15:57:07.904355+0900 map[12058:915339] Could not inset legal attribution from corner 4 2---------------------- 3<MKMapItem: 0x604000158100> { 4 isCurrentLocation = 0; 5 name = "Unknown Location"; 6 placemark = "<+37.50910181,-122.34185368> +/- 0.00m, region CLCircularRegion (identifier:'<+37.50910181,-122.34185368> radius 0.00', center:<+37.50910181,-122.34185368>, radius:0.00m)"; 7} 8<MKMapItem: 0x6040003411e0> { 9 isCurrentLocation = 0; 10 name = "Unknown Location"; 11 placemark = "<+35.65861100,+139.74555600> +/- 0.00m, region CLCircularRegion (identifier:'<+35.65861100,+139.74555600> radius 0.00', center:<+35.65861100,+139.74555600>, radius:0.00m)"; 12} 13---------------------- 14<<MKDirectionsRequest: 0x6000002b2540> source:(null) destination:(null) transportType:268435455> 15---------------------- 16

###ソースコード全文
作成中のアプリではトラッキングモードの変更や地図の切り替え等も実装されています。

swift

1import UIKit 2import MapKit 3import CoreLocation 4 5class ViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate { 6 7// 目的地 8 var destLocation = CLLocationCoordinate2D(latitude: 35.658611, longitude: 139.745556) 9 10// 現在地 11 var userLocation: CLLocationCoordinate2D! 12 13 14 // マップビュー 15 @IBOutlet weak var myMap: MKMapView! 16 // ツールバー 17 @IBOutlet weak var toolBar: UIToolbar! 18 // ツールバーのTintColorの初期値 19 var defaultColor:UIColor! 20 21 var locationManager = CLLocationManager() 22 23 @IBOutlet weak var trackingButton: UIBarButtonItem! 24 25 // 地図のタイプを切り替える 26 @IBAction func changedMapType(_ sender: UISegmentedControl) { 27 switch sender.selectedSegmentIndex { 28 case 0 : 29 // 地図 30 myMap.mapType = .standard 31 // 俯角(見下ろす角度) 32 myMap.camera.pitch = 0.0 33 // ツールバーを標準に戻す 34 toolBar.tintColor = defaultColor 35 toolBar.alpha = 1.0 36 case 1 : 37 // 衛星写真 38 myMap.mapType = .satellite 39 // ツールバーを黒文字、背景半透明にする 40 toolBar.tintColor = UIColor.black 41 toolBar.alpha = 0.8 42 case 2 : 43 // 写真+地図(ハイブリッド) 44 myMap.mapType = .hybrid 45 // ツールバーを黒文字、背景半透明にする 46 toolBar.tintColor = UIColor.black 47 toolBar.alpha = 0.8 48 case 3: 49 // 地図 50 myMap.mapType = .standard 51 // ツールバーを標準に戻す 52 toolBar.tintColor = defaultColor 53 toolBar.alpha = 1.0 54 // 3Dビュー 55 myMap.camera.pitch = 70 // 俯角(見下ろす角度) 56 myMap.camera.altitude = 700 // 標高 57 default: 58 break 59 } 60 } 61 62 // トラッキングモードを切り替える 63 @IBAction func tapTrackingButton(_ sender: UIBarButtonItem) { 64 getRoute() 65 switch myMap.userTrackingMode { 66 case .none: 67 // noneからfollowへ 68 myMap.setUserTrackingMode(.follow, animated: true) 69 // トラッキングボタンを変更する 70 trackingButton.image = UIImage(named: "trackingFollow") 71 case .follow: 72 // followからfollowWithHeadingへ 73 myMap.setUserTrackingMode(.followWithHeading, animated: true) 74 // トラッキングボタンを変更する 75 trackingButton.image = UIImage(named: "trackingHeading") 76 case .followWithHeading: 77 // followWithHeadingからnoneへ 78 myMap.setUserTrackingMode(.none, animated: true) 79 // トラッキングボタンを変更する 80 trackingButton.image = UIImage(named: "trackingNone") 81 } 82 } 83 84 // トラッキングが自動解除された 85 func mapView(_ mapView: MKMapView, didChange mode: MKUserTrackingMode, animated: Bool) { 86 // トラッキングボタンを変更する 87 trackingButton.image = UIImage(named: "trackingNone") 88 } 89 90 // 位置情報利用許可のステータスが変わった 91 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 92 93 userLocation = CLLocationCoordinate2DMake(manager.location!.coordinate.latitude, manager.location!.coordinate.longitude) 94 95 // 現在地の取得を開始 96 self.locationManager.startUpdatingLocation() 97 98 99 switch status { 100 case .authorizedAlways, .authorizedWhenInUse : 101 // ロケーションの更新を開始 102 locationManager.startUpdatingLocation() 103 // トラッキングボタンを有効 104 trackingButton.isEnabled = true 105 default: 106 // ロケーションの更新を停止 107 locationManager.stopUpdatingLocation() 108 // トラッキングモードをnoneに 109 myMap.setUserTrackingMode(.none, animated: true) 110 //トラッキングボタンを変更 111 trackingButton.image = UIImage(named: "trackingNone") 112 // トラッキングボタンを無効にする 113 trackingButton.isEnabled = false 114 } 115 } 116 117 118 119 120 override func viewDidLoad() { 121 super.viewDidLoad() 122 // スケールを表示する 123 myMap.showsScale = true 124 125 locationManager.requestWhenInUseAuthorization() 126 127 locationManager.delegate = self 128 129 myMap.delegate = self 130 131 132 } 133 134 override func didReceiveMemoryWarning() { 135 super.didReceiveMemoryWarning() 136 // Dispose of any resources that can be recreated. 137 } 138 139 140 141 func getRoute() 142 { 143 144 145 // 現在地と目的地のMKPlacemarkを生成 146 var fromPlacemark = MKPlacemark(coordinate:userLocation, addressDictionary:nil) 147 var toPlacemark = MKPlacemark(coordinate:destLocation, addressDictionary:nil) 148 149 150 151 // MKPlacemark から MKMapItem を生成 152 var fromItem = MKMapItem(placemark:fromPlacemark) 153 var toItem = MKMapItem(placemark:toPlacemark) 154 print("----------------------") 155 print(fromItem) 156 print(toItem) 157 print("----------------------") 158 // MKMapItem をセットして MKDirectionsRequest を生成 ミス 159 let myRequest = MKDirectionsRequest() 160 print(myRequest) 161 print("----------------------") 162 163 164 165 166 myRequest.source = fromItem 167 myRequest.destination = toItem 168 169 myRequest.requestsAlternateRoutes = false 170 myRequest.transportType = MKDirectionsTransportType.walking 171 172 let directions = MKDirections(request: myRequest) 173 directions.calculate(completionHandler: {(response, error) -> Void in 174 if error != nil { 175 return 176 } 177 guard var response = response else { 178 return 179 } 180 if response.routes.isEmpty { 181 return 182 } 183 var route: MKRoute = response.routes[0] as MKRoute 184 // 経路を描画 185 self.myMap.add(route.polyline) 186 187 }) 188 func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 189 let route: MKPolyline = overlay as! MKPolyline 190 let routeRenderer = MKPolylineRenderer(polyline:route) 191 routeRenderer.lineWidth = 5.0 192 routeRenderer.strokeColor = UIColor.red 193 return routeRenderer 194 } 195 } 196}

###参考にしたページ
https://qiita.com/yuriken27@github/items/6362bfaa93548b643aa6
###補足情報(言語/FW/ツール等のバージョンなど)
swift4.0
xcode9.0

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問