swift MapKit注释拖拽状态图标

时间:2021-02-10 21:17:03

I have run into a slight problem. I am trying to use a custom icon for my mapView annotation. The trouble is that when the user drags the icon it always changes back to the default icon.

我遇到了一个小问题。我正在尝试为我的mapView注释使用一个自定义图标。问题是当用户拖拽图标时,它总是会切换回默认图标。

I set the icon image in my mapView delegate like so, this works to set the icon.

我在mapView委托中设置图标图像,就像这样,这是用来设置图标的。

// MARK: - Map Annotations
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation{
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

    if(pinView == nil){
        if let customAnnot = annotation as? myAnnotation {
            pinView = MKPinAnnotationView(annotation: customAnnot, reuseIdentifier: reuseId)


            pinView!.image = UIImage(named:"pin-50.png")


            pinView!.animatesDrop = false
            pinView!.draggable = true
        }

    } else {
        pinView!.annotation = annotation as? myAnnotation
    }

    return pinView!
}

I tried a few things to fix but none have seem to helped. even when I try to set the icon again in the "didChangeDragState" delegate it still changes to default icon.

我试了几样东西去修复,但似乎没有一件对我有帮助。即使我尝试在“didChangeDragState”委托中再次设置图标,它仍然会更改为默认图标。

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    if newState == MKAnnotationViewDragState.Dragging {
        println("draggin it")
        view.image = UIImage(named:"pin-50.png")
    }

    if newState == MKAnnotationViewDragState.Ending {
        //update pin location
        if let customAnnot = view.annotation as? myAnnotation {
            cData.updatePinLocation(customAnnot.pinID, newValue: customAnnot.coordinate)
        }
        view.image = UIImage(named:"pin-50.png")

    }

    if newState == MKAnnotationViewDragState.Starting {
        println("start drag")
        view.image = UIImage(named:"pin-50.png")
    }

}

1 个解决方案

#1


2  

Thanks to zisoft, I figured it out. here is the code that works

多亏了zisoft,我找到了答案。这是有效的代码

   if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

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

        pinView.annotation = annotation
    }

    return pinView

#1


2  

Thanks to zisoft, I figured it out. here is the code that works

多亏了zisoft,我找到了答案。这是有效的代码

   if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)

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

        pinView.annotation = annotation
    }

    return pinView