I have a UIViewController that contains a MKMapView. I have added an annotation at my current location by the following code:
import UIKit
import MapKit
class LocationViewController: UIViewController , MKMapViewDelegate ,CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!
let regionRadius: CLLocationDistance = 1000
var token: dispatch_once_t = 0
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
if ((userLocation.coordinate.latitude != 0.0) && (userLocation.coordinate.longitude != 0.0)) {
dispatch_once(&token) {
self.centerMapOnLocation(userLocation.location!)
let annotation = MapPin(title: "This Location", locationName: "Test", discipline: "None", coordinate: userLocation.coordinate)
mapView.addAnnotation(annotation)
}
}
}
I would like to make the annotation move as the the map view is being moved or dragged by the user. For example my current location is New Albany and if i drag the map not the annotation is will change its stage floating in mid air till i release on top of Pontotoc so the annotation points where i released. I would be grateful for any hits or good tutorials on MapKit.