Sourabh Sharma
Sourabh Sharma

Reputation: 8322

How to make annotation point draggable in MapKit using swift 2 and xcode 7?

It is very frustrating to find the solution for this problem. I posted the whole code to understand the problem. Please help

import UIKit
import MapKit
import CoreLocation
class LocationViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate {


    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()
    var annotationPoint: MKPointAnnotation!
    var getMovedMapCenter: CLLocation!
    var myPinView:MKPinAnnotationView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if #available(iOS 8.0, *) {
            self.locationManager.requestWhenInUseAuthorization()
        } else {
            // Fallback on earlier versions
        }
        self.locationManager.startUpdatingLocation()
        self.mapView.showsUserLocation = true

        // Do any additional setup after loading the view.
    }

    // MARK: - Location Delegate Methods

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations.last

        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

        self.mapView.setRegion(region, animated: true)

        if(self.annotationPoint == nil)
        {
            self.annotationPoint = MKPointAnnotation()
            getMovedMapCenter =  CLLocation(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

             self.annotationPoint.coordinate = getMovedMapCenter.coordinate

            // self.annotationPoint.title = location_value
            //self.annotationPoint.subtitle = ""

            // println_debug(self.annotationPoint)

            self.mapView.addAnnotation(self.annotationPoint)

        }

        self.locationManager.stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print("Errors: " + error.localizedDescription)
    }

    //MARK:- View for Annotation
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
    {
        if annotation is MKPointAnnotation {
            let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

            pinAnnotationView.pinColor = .Purple
            pinAnnotationView.draggable = true
            pinAnnotationView.canShowCallout = true
            pinAnnotationView.animatesDrop = true

            return pinAnnotationView
        }

        return nil
    }

    //MARK:- Annotation Changed State

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {

        print("hiii")


        //        println_debug("End State coordinates: \(self.annotationPoint.coordinate.latitude),\(self.annotationPoint.coordinate.longitude)")


        if(newState == .Ending)
        {


            let center = CLLocationCoordinate2D(latitude: self.annotationPoint.coordinate.latitude, longitude: self.annotationPoint.coordinate.longitude)
            //    self.currentLocationNameA(center)

            //            dispatch_async(GlobalBackgroundQueue)
            //                {
            //
            //
            //            }
        }

    }

    //MARK:- update Not Get

    func mapView(mapView: MKMapView,
        didFailToLocateUserWithError error: NSError)
    {
        // AppHelper.showALertWithTag(121, title: APP_NAME, message: "Failed to get location. Please check Location setting", delegate: nil, cancelButtonTitle: "Ok", otherButtonTitle: nil)

    }




}

I got stuck in this code. Becasuse didChangeDragState Delegate never called. Please help me to find a better solution for this. I am new to swift; So its hard to convert the objective c code in swift

Upvotes: 2

Views: 3134

Answers (3)

Sourabh Sharma
Sourabh Sharma

Reputation: 8322

 pinAnnotationView.canShowCallout = false

This line solves my problem. Popup of showing "current location" was the main cause of this problem.

and these changes in code make it smooth dragging. Mehul and Vijay Answer help me in finding this solution.

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
        switch (newState) {
        case .Ending, .Canceling:
            view.dragState = .None
        default: break
        }
    }

Upvotes: 1

Mehul
Mehul

Reputation: 3178

The drag state typically changes in response to user interactions with the annotation view. However, the annotation view itself is responsible for changing that state as well.

replace this method and try once. may be working fine

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    switch (newState) {
    case .Starting:
        view.dragState = .Dragging
    case .Ending, .Canceling:
        view.dragState = .None
    default: break
    }
}

Replace you point annotation method with this one.

let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = placemark.name //or whatever title you like
self.mapView.addAnnotation(pa)

for more , you can see Code and Another Sample Code

Upvotes: 2

vijay
vijay

Reputation: 165

   if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId12 = "pin12"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

    if pinView == nil {
        pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId12)
        pinView.image = UIImage(named:"pin-1250.png")
        pinView.canShowCallout = false
        pinView.draggable = true
    }
    else {

        pinView.annotation = annotation
    }

    return pinView

Try this code.

Upvotes: 0

Related Questions