2

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.

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

// 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.

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")
    }

}
BpRocker
  • 41
  • 4
  • 1
    You are using `MKPinAnnotationView`, which draws the default needle. You should use or subclass `MKAnnotationView` to handle your own view. – zisoft May 04 '15 at 09:06

1 Answers1

2

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

   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
BpRocker
  • 41
  • 4