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

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

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

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

Swift

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

Q&A

1回答

3232閲覧

Mapsでルートのシミュレーションがしたい

interpiamobile

総合スコア66

iOS

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

Swift

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

0グッド

1クリップ

投稿2016/06/30 06:57

現在MapKit.frameworkを使って地図機能を実装しているのですが、目的地にピンを留めた後に現在地からその目的地までのルートをシミュレーションしたいです。しかしどうすればいいか分かりません。
詳しくは以下のページにある動画のようにしたいです。(3Dでなくて2Dで構いません)

【裏技】Googleマップのルート検索で「3D」をオンにするとアニメーションが見られます

この動画のようにルートに沿ってピンを走らせるにはどうすればいいのでしょうか?
現在以下のようにコードを実装しており、現在地から目的地までの線引き等はできています。

Swift

1class ViewController: UIViewController, CLLocationManagerDelegate { 2 3 @IBOutlet weak var mapView: MKMapView! 4 var lm: CLLocationManager! = nil 5 var fromCoordinate: CLLocationCoordinate2D? 6 var toCoordinate: CLLocationCoordinate2D? 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 setCoreLocation() 12 addLongPressGesture() 13 } 14 15 override func didReceiveMemoryWarning() { 16 super.didReceiveMemoryWarning() 17 } 18 19 //MARK: ViewDidLoadPreference 20 21 func setCoreLocation() { 22 lm = CLLocationManager() 23 lm.delegate = self 24 25 lm.requestAlwaysAuthorization() 26 lm.desiredAccuracy = kCLLocationAccuracyBest 27 lm.distanceFilter = 300 28 lm.startUpdatingLocation() 29 } 30 31 func addLongPressGesture() { 32 let myLongPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPressGesture(_:))) 33 myLongPressGesture.minimumPressDuration = 1.0 34 myLongPressGesture.allowableMovement = 150 35 36 mapView.addGestureRecognizer(myLongPressGesture) 37 } 38 39 //MARK: GestureRecognizer 40 41 func longPressGesture(sender: UILongPressGestureRecognizer) { 42 if sender.state != UIGestureRecognizerState.Ended { 43 let touchLocation = sender.locationInView(mapView) 44 let locationCoordinate = mapView.convertPoint(touchLocation,toCoordinateFromView: mapView) 45 toCoordinate = CLLocationCoordinate2D(latitude: locationCoordinate.latitude, longitude: locationCoordinate.longitude) 46 47 setPointAnnotation(toCoordinate!) 48 49 routeSearch() 50 } 51 } 52 53 //MARK: CoreLocationDelegate 54 55 func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation){ 56 fromCoordinate = CLLocationCoordinate2D(latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude) 57 58 guard let coodinate = fromCoordinate else { 59 return 60 } 61 62 let region = MKCoordinateRegionMake(coodinate, MKCoordinateSpanMake(0.01, 0.01)) 63 mapView.setRegion(region, animated:true) 64 mapView.userTrackingMode = MKUserTrackingMode.FollowWithHeading 65 } 66 67 func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 68 NSLog("Error") 69 } 70 71 //MARK: MapKit 72 73 func routeSearch() { 74 let fromPlacemark = MKPlacemark(coordinate:fromCoordinate!, addressDictionary:nil) 75 let toPlacemark = MKPlacemark(coordinate:toCoordinate!, addressDictionary:nil) 76 77 let fromItem = MKMapItem(placemark:fromPlacemark); 78 let toItem = MKMapItem(placemark:toPlacemark); 79 80 let request = MKDirectionsRequest() 81 request.source = fromItem 82 request.destination = toItem 83 request.requestsAlternateRoutes = true; 84 request.transportType = MKDirectionsTransportType.Walking 85 86 let directions = MKDirections(request: request) 87 directions.calculateDirectionsWithCompletionHandler { [unowned self] response, error in 88 guard let unwrappedResponse = response else { return } 89 90 for route in unwrappedResponse.routes { 91 self.mapView.addOverlay(route.polyline) 92 self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true) 93 94 self.title = String(Int(round(route.distance / 100))) + "km " + String(Int(round(route.expectedTravelTime / 60))) + "分" 95 } 96 } 97 } 98 99 func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 100 let route: MKPolyline = overlay as! MKPolyline 101 let routeRenderer = MKPolylineRenderer(polyline:route) 102 routeRenderer.lineWidth = 5.0 103 routeRenderer.strokeColor = UIColor.redColor() 104 return routeRenderer 105 } 106 107 func setPointAnnotation(coordinate: CLLocationCoordinate2D) { 108 let annotation = MKPointAnnotation() 109 annotation.coordinate = coordinate 110 self.mapView.addAnnotation(annotation) 111 } 112 113}

どうすればルートのシミュレーションをすることができるでしょうか?
どなたかわかる方がいれば教えていただきたいです。よろしくお願いします。

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

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

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

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

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

guest

回答1

0

MapKit や Google Maps SDK for iOS には、「ルートのシミュレーション」を行う機能はないと思われますので、それを行うには地道に実装するしかないと思います。

具体的には、

  1. タイマーで1秒置きなどで、以下の処理を行う
  2. MKDirection で得られたルート探索結果 route.polyline の座標を順に取る
  3. その座標に車などのマーカーを表示させる&地図をその位置に移動させる

となると思います。

「いい感じ」にシミュレーションしているように見せるには、

  • 車のマーカーの向き(角度)または地図の向き
  • 車の走行速度(km/h)の考慮
  • 地図の表示領域

などを考慮しないといけないので、より複雑なプログラムになると思います。

他の案としては、

で、GoogleマップアプリやiOSのマップアプリを呼び出す方法がありますが、これが「シミュレーション」の要件を満たすかは、確認されるとよろしいかと思います。

投稿2016/07/06 03:00

amay077

総合スコア1075

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問