Jordi Jordi - 6 months ago 45
Swift Question

Custom pin image in annotationView in iOS 9

I'm trying to change from Swift 1.2 to Swift 2 and I'm at the end of the changes. Currently I'm making changes in the MapViewController, and there isn't any error or warning, but the custom image for my pin (annotationView) it's not assigned to the pin and it's showing the default one (red dot).

Here is my code, I hope you can help me with some tip because I think everything is fine but it's still not working:

func parseJsonData(data: NSData) -> [Farmacia] {

let farmacias = [Farmacia]()

do
{
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

// Parse JSON data
let jsonProductos = jsonResult?["farmacias"] as! [AnyObject]

for jsonProducto in jsonProductos {

let farmacia = Farmacia()
farmacia.id = jsonProducto["id"] as! String
farmacia.nombre = jsonProducto["nombre"] as! String
farmacia.location = jsonProducto["location"] as! String

let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(farmacia.location, completionHandler: { placemarks, error in

if error != nil {
print(error)
return
}

if placemarks != nil && placemarks!.count > 0 {

let placemark = placemarks?[0]

// Add Annotation
let annotation = MKPointAnnotation()
annotation.title = farmacia.nombre
annotation.subtitle = farmacia.id
annotation.coordinate = placemark!.location!.coordinate

self.mapView.addAnnotation(annotation)
}

})
}
}
catch let parseError {
print(parseError)
}

return farmacias
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

let identifier = "MyPin"

if annotation.isKindOfClass(MKUserLocation) {
return nil
}

// Reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

if annotationView == nil
{
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
}

annotationView!.image = UIImage(named: "custom_pin.png")

let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
annotationView!.rightCalloutAccessoryView = detailButton

print(annotationView!.image)

return annotationView
}


Thanks in advance,

Regards.

Answer

Here is the answer:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)

    if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "custom_pin.png")
        annotationView.rightCalloutAccessoryView = detailButton
    }
    else {
        annotationView.annotation = annotation
    }

    return annotationView
}

Regards

Comments