現在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}
どうすればルートのシミュレーションをすることができるでしょうか?
どなたかわかる方がいれば教えていただきたいです。よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。