How to drag annotation while dragging MKMapView? (IOS)

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

+4
4

-, . :

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
}

, MKMapViewDelegate , :

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    if newState == MKAnnotationViewDragState.Ending {
        let droppedAt = view.annotation?.coordinate
        print(droppedAt)
    }
}

. .

:

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)
}

pinView?.animatesDrop = true viewForAnnotation.

enter image description here

+8

. : , / MKMapView, UIPanGestureRecognizer . , .

+1

, , :

var mapRegionTimer: Timer?
public func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
    mapRegionTimer?.invalidate()
    mapRegionTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { (t) in
        self.myAnnotation.coordinate = CLLocationCoordinate2DMake(mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude);
        self.myAnnotation.title = "Current location"
        self.mapView.addAnnotation(self.myAnnotation)
    })
}
public func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    mapRegionTimer?.invalidate()
}
0
source

You cannot get from here.

I hope this will be fixed in a later version of iOS.

The problem seems to be related to background updating.

It’s very annoying that iOS cannot do what JavaScript and AJAX do so easily.

-1
source

Source: https://habr.com/ru/post/1612060/


All Articles