MKAnnotation Pin 不会拖动,即使设置为可拖动



我一直在尝试在MapView上放置一个可拖动的注释。(我使用的是默认引脚,而不是我自己的PIN)到目前为止,我只能在设定的坐标(实际上并没有太多的成就)上显示它,我需要首先获得注释以对被选中的反应,它不是didChangeDragState Func曾经收到过。然后,我需要能够将其拖动,将其放在新位置并获取新位置的坐标。

我对Swift来说是合理的新手,但是我已经接受了一个相当困难的项目。我已经在Google上查看了几乎所有可以找到的所有内容,寻找" Swift中的MKAnnotation MAPKIT"和类似的变体。(编辑:我没有找到任何答案来阐明我的问题,所有其他答案给出了如何上传个性化MKAnnotation的回答。他们都有标题字段,但是没有一个答案提到标题字段是必要的,这是主要问题。PIN的移动,但是在我的情况下,这是不正确的,如下所示)无论如何!以下是我尝试实现mapView并添加注释的代码。

var currentLat:CLLocationDegrees!
var currentLong:CLLocationDegrees!
var currentCoordinate:CLLocationCoordinate2D! 
....
override func viewDidAppear(animated: Bool) {
    let annotation = PinAnnotationClass()
    annotation.setCoordinate(currentCoordinate)
    //annotation.setCoordinate(currentCoordinate)
    //AnnotationView()
    self.mapView.addAnnotation(annotation)
}
override func viewDidLoad() {
    super.viewDidLoad()
    println("hello!")
    self.mapView.delegate = self
    loadMap()
    findPath()
}
func loadMap()
{
    currentCoordinate = CLLocationCoordinate2DMake(currentLat, currentLong)
    var mapSpan = MKCoordinateSpanMake(0.01, 0.01)
    var mapRegion = MKCoordinateRegionMake(currentCoordinate, mapSpan)
    self.mapView.setRegion(mapRegion, animated: true)
}

以及扩展名:

extension DetailsViewController: MKMapViewDelegate {    
func mapView(mapView: MKMapView!,
    viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }
            let reuseId = "pin"
            var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
            if pinView == nil {
                pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                pinView!.canShowCallout = true
                pinView!.draggable = true
                pinView!.annotation.coordinate
                pinView!.animatesDrop = true
                pinView!.pinColor = .Green
            }
            else {
                pinView!.annotation = annotation
            }
            return pinView
}
  func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    if (newState == MKAnnotationViewDragState.Starting) {
        view.dragState = MKAnnotationViewDragState.Dragging
    } else if (newState == MKAnnotationViewDragState.Ending || newState == MKAnnotationViewDragState.Canceling){
        view.dragState = MKAnnotationViewDragState.None
    }
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if let annotation = view.annotation as? PinAnnotationClass{
    }
}

我也有一个自定义的Pinannotation类:

import Foundation
import MapKit
class PinAnnotationClass : NSObject, MKAnnotation {
private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
var coordinate: CLLocationCoordinate2D {
    get {
        return coord
    }
}
var title: String = ""
var subtitle: String = ""
func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
    self.coord = newCoordinate
}

最初的问题是未设置注释的title

如果未设置注释的title,则无法将其设置为"选定"状态,并且不会显示标注,并且didSelectAnnotationView不会被调用。

要拖动注释,您必须首先选择它,如果未设置title,则无法将其拖动。

因此,在创建注释时,将其title设置为某物:

let annotation = PinAnnotationClass()
annotation.title = "hello"
annotation.setCoordinate(currentCoordinate)


至少应该让您开始拖动它,但是由于您使用的是MKPinAnnotationView而不是普通的MKAnnotationView,因此您需要不是需要实现didChangeDragState。实际上,如果使用MKPinAnnotationView时确实实现了它,则注释不会正确拖动。

didChangeDragState中,删除设置view.dragState的代码 - 使用MKPinAnnotationView时不需要它。

相反,您可以只记录删除注释的坐标:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    if newState == MKAnnotationViewDragState.Ending {
        let ann = view.annotation
        print("annotation dropped at: (ann!.coordinate.latitude),(ann!.coordinate.longitude)")
    }
}


无关,但是PinAnnotationClass实现比需要的要复杂。您无需为coordinate编写明确的获取方法并设置方法。只需声明 coordinate,getter/setter就会为您完成:

class PinAnnotationClass : NSObject, MKAnnotation {
    var title: String = ""
    var subtitle: String = ""
    var coordinate: CLLocationCoordinate2D = kCLLocationCoordinate2DInvalid
    //kCLLocationCoordinate2DInvalid is a pre-defined constant
    //better than using "0,0" which are technically valid
}

您还需要更改分配坐标的方式:

//annotation.setCoordinate(currentCoordinate)  //old
annotation.coordinate = currentCoordinate      //new


最后,也无关,但是viewForAnnotation中的这条线

pinView!.annotation.coordinate

表示并且什么都不做,删除它。

最新更新