Reputation: 500
I watched a WWDC session on Apple Mapkit
https://developer.apple.com/videos/play/wwdc2015/206/
I want to make a flyover animation showed at 31:33. Change camera angle from top to bottom and spin. But I wrote some code, it crashes. Please help me and tell me what's wrong with my code, thanks!
import UIKit
import CoreLocation
import MapKit
class Flyover: UIViewController, MKMapViewDelegate {
var coordinate = CLLocationCoordinate2D(latitude: 40.7484405, longitude: -73.9856644)
let distance : CLLocationDistance = 700
let pitch : CGFloat = 65
let heading = 90.0
var camera: MKMapCamera?
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.mapType = .SatelliteFlyover
let camera = MKMapCamera(lookingAtCenterCoordinate: coordinate,fromDistance: distance,pitch: pitch,heading: heading)
mapView.camera = camera
//I want the animation to play automatically as soon as the user is in the view.
UIView.animateWithDuration(10.0, animations: {
self.camera!.heading += 180
self.camera!.pitch = 25
self.mapView.camera = self.camera!
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Upvotes: 1
Views: 919
Reputation: 850
You were almost there. Try making a new camera like this.
mapView.camera = MKMapCamera(lookingAtCenter: pinLocation, fromDistance: 2000, pitch: 0, heading: 0)
let pinLocation = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let rotationCamera = MKMapCamera(lookingAtCenter: pinLocation, fromDistance: 2000, pitch: 75, heading: 180)
UIView.animate(withDuration: 10, delay: 0, options: .curveEaseInOut, animations: {
self.mapView.camera = rotationCamera
}, completion: nil)
Upvotes: 4