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