I have a UIViewController that contains MKMapView. I added the annotation at my current location with 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 when the map image is moved or dragged by the user. For example, my current location is New Albany, and if I dragged the map, the annotation will not change its scene floating in the air until I get to the top of the Pontotoc, so the annotation points that I released. I would be grateful for any hits or good lessons on MapKit.
http://i.stack.imgur.com/xGK4R.jpg
http://i.stack.imgur.com/4MQIR.jpg