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

The*_*ian 10 mkpinannotationview mkmapview ios mkmapviewdelegate swift

我一直试图在mapview上放置一个可拖动的注释多年.(我正在使用默认的引脚,而不是我自己的引脚)到目前为止,我只能在设定的坐标处显示它(实际上并不是很大的成就),我需要首先获得注释以对选择作出反应,它不会永远不会被这个didChangeDragState功能所接受.然后我需要能够拖动它,将它放在一个新的位置并获得新位置的坐标.

我对Swift来说是个新手,但我已经开始了一个相当困难的项目.我看了几乎所有我在google上寻找" MKAnnotationSwift中的可拖动mapkit"和类似变体的东西.(编辑:我没有找到任何解释我的问题的答案,所有其他答案都提供了如何上传个性化的答案MKAnnotation.他们都有标题字段,但没有一个答案提到标题字段是必要的,结果证明是主要的问题.他们只提到我应该设置dragState来控制引脚的移动,但在我的情况下,这看起来是不正确的,如下所示)无论如何 !下面是我的代码,我尝试实现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)
}
Run Code Online (Sandbox Code Playgroud)

随着扩展:

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{
    }
}
Run Code Online (Sandbox Code Playgroud)

我还有一个自定义的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
}
Run Code Online (Sandbox Code Playgroud)

小智 14

最初的问题title是没有设置注释.

如果title没有设置注释,则不能将其设置为"已选择"状态,并且它不会显示标注,didSelectAnnotationView也不会被调用.

由于要拖动注释,您必须先选择它,如果title未设置,则无法拖动它.

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

let annotation = PinAnnotationClass()
annotation.title = "hello"
annotation.setCoordinate(currentCoordinate)
Run Code Online (Sandbox Code Playgroud)


这至少应该让你开始拖动它,但由于您使用的是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)")
    }
}
Run Code Online (Sandbox Code Playgroud)


不相关,但PinAnnotationClass实施比它需要的更复杂.您不需要为其编写显式的get和set方法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
}
Run Code Online (Sandbox Code Playgroud)

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

//annotation.setCoordinate(currentCoordinate)  //old
annotation.coordinate = currentCoordinate      //new
Run Code Online (Sandbox Code Playgroud)


最后,也不相干但这一行 viewForAnnotation

pinView!.annotation.coordinate
Run Code Online (Sandbox Code Playgroud)

意味着什么也不做,删除它.