Change image of Pins in Swift (MapView)

1

I have an App where I send to place locations with their respective Annotations . I managed to change the image of the pin, but I see that all the images are changed.

What I would like to do is show a different image for each group of locations, for example, I have hospitals and pharmacies, then I would like to put a red cross in Hospital and a medicine in a pharmacy.

For now I only have the crosses and I have been investigating but I am still unsuccessful. I hope you can help me.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")
    if annotationView == nil{
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
        annotationView!.canShowCallout = true
        annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

        let pinImg = UIImage(named: "curz")
        let size = CGSize(width: 50, height: 50)
        UIGraphicsBeginImageContext(size)
        pinImg!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        annotationView!.image = resizedImage
    }
    else {
        annotationView!.annotation = annotation
    }
    return annotationView
}
    
asked by Daniel C. 29.05.2017 в 19:29
source

1 answer

2

What you could do is have a subclass of MKAnnotation where you store information you need to know what type it is.

Something like this:

enum CustomAnnotationType {
    case hospital
    case farmacia
}

class CustomAnnotation: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var type: CustomAnnotationType

    init(coordinate: CLLocationCoordinate2D, type: CustomAnnotationType) {
        self.coordinate = coordinate
        self.type = type
    }
}

When you add annotations to the map, you tell it what it is:

let customAnnotation = CustomAnnotation(coordinate: coord, type: .farmacia)
map.addAnnotation(customAnnotation)

and when you are going to create the view for a specific annotation , you use the corresponding image for each one:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
    if let customAnnotation = annotation as? CustomAnnotation {
        let identifier = identifier(forCustomAnnotationType: customAnnotation.type)
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")
        if annotationView == nil{
            let imageName = imageName(forCustomAnnotationType: customAnnotation.type)
            ...
    }
    ...
}

Note: identifier(forCustomAnnotationType: ) e imageName(forCustomAnnotationType: ) are functions that you should implement with a switch ...

    
answered by 29.05.2017 / 20:41
source