如何使批注点可拖动在 MapKit 使用 swift 2 和 xcode 7?

标签: ios Swift
发布时间: 2016/4/28 20:44:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这是非常令人沮丧,为此问题找到解决办法。我张贴整个代码要了解的问题。请帮助

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 代码中swift

解决方法 1:

 pinAnnotationView.canShowCallout = false

这条线可以解决我的问题。弹出窗口的显示"当前位置"是这一问题的主要原因。

和这些代码中的更改使其平滑拖动。梅于尔和维贾伊 · 答案帮我找到这个解决办法。

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
        switch (newState) {
        case .Ending, .Canceling:
            view.dragState = .None
        default: break
        }
    }
官方微信
官方QQ群
31647020