Ios 如何使用swift 2和xcode 7在MapKit中拖动注释点?

Ios 如何使用swift 2和xcode 7在MapKit中拖动注释点?,ios,swift,mapkit,mkpointannotation,Ios,Swift,Mapkit,Mkpointannotation,找到这个问题的解决办法是非常令人沮丧的。我发布了完整的代码来理解这个问题。请帮忙 import UIKit import MapKit import CoreLocation class LocationViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet weak var mapView: MKMapView! let locationManage

找到这个问题的解决办法是非常令人沮丧的。我发布了完整的代码来理解这个问题。请帮忙

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)

    }




}
我被困在这个密码里了。因为didChangeDragState委托从未调用过。请帮我找到更好的解决方案。我不熟悉斯威夫特;因此,很难在swift中转换目标c代码

   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
试试这个代码

拖动状态通常会随着用户与的交互而更改 注释视图。但是,注释视图本身是 负责改变这种状态

替换此方法并重试一次。可能工作正常

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
    }
}
将点批注方法替换为此方法。

let pa = MKPointAnnotation()
pa.coordinate = placemark.location.coordinate
pa.title = placemark.name //or whatever title you like
self.mapView.addAnnotation(pa)
更多信息,请参见和 这条线解决了我的问题。显示“当前位置”的弹出窗口是该问题的主要原因

代码中的这些更改使它能够顺利拖动。Mehul和Vijay回答帮助我找到这个解决方案

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

但这种方法并不是在召唤。这是您在设置mapView委托时错过的
viewDidLoad()
方法中出现的主要问题。尝试添加
self.mapView.delegate=self
delegate是通过故事板设置的。别针没有移动,只有地图在移动汉克斯·梅胡尔。我正在尝试使用示例代码。希望能帮上忙示例不起作用。由于CoordinationHKEY的值为零,它在按钮操作中崩溃,我们必须尝试另一种解决方案。给我时间。只要我有空,我会给你完美的解决方案。我在等你的帮助。谢谢你的善意帮助
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
        switch (newState) {
        case .Ending, .Canceling:
            view.dragState = .None
        default: break
        }
    }