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.
First, you have to return a view for your annotation. You can do so like this:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.draggable = true
}
else {
pinView?.annotation = annotation
}
return pinView
}
Then, once you have adopted the MKMapViewDelegate protocol in your controller, you grab the coordinates of a pin after dragging it like this:
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
if newState == MKAnnotationViewDragState.Ending {
let droppedAt = view.annotation?.coordinate
print(droppedAt)
}
}
See also my sample code.
EDIT
alternative solution:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Remove all annotations
self.mapView.removeAnnotations(mapView.annotations)
// Add new annotation
let annotation = MKPointAnnotation()
annotation.coordinate = mapView.centerCoordinate
annotation.title = "title"
annotation.subtitle = "subtitle"
self.mapView.addAnnotation(annotation)
}
Don't forget pinView?.animatesDrop = true
in viewForAnnotation
.
There is another solution. In this post: determine if MKMapView was dragged/moved, the answer with the most votes uses a UIPanGestureRecognizer on the map view. You could use this to move the pin AS you drag the map.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With