Swift Firebase download annotations images on map annotation click

368 views Asked by At

With code below i change the appearance of my pins and download also an image from firebase.

The problem is that i need that the image is downloaded only when a pin is pressed, now instead this comes all in once.

Can someone tell me how can i detect pressed pin and download only his image?

I'm new to programming...

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

        if !(annotation is SxAnnotations){
            return nil
        }

            // If no pin view already exists, create a new one.
            let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            annotationView.pinTintColor = .green
                annotationView.animatesDrop = true
            annotationView.canShowCallout = true
            // Because this is an iOS app, add the detail disclosure button to display details about the annotation in another view.


        let sxAnnotations = annotation as! SxAnnotations

        let downloadURL = sxAnnotations.image!

        storageRef.storage.reference(forURL: downloadURL).data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                    let image = UIImage(data: data!)

                annotationView.detailCalloutAccessoryView = UIImageView(image: image)
                    let rightAccessory = UILabel(frame: CGRect(x:0,y:0,width:50,height:30))
                    rightAccessory.text = sxAnnotations.name!
                    rightAccessory.font = UIFont(name: "Verdana", size: 10)
                    annotationView.rightCalloutAccessoryView = rightAccessory

            })
        return annotationView;
        }

edit: i know that there is a func:

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
    //Pin clicked, do your stuff here
}

but i'm not able to implement download in that

1

There are 1 answers

0
Jay On

If you are tapping a pin, one option is to use a delegate function to then add behavior, like changing the image.

When the pin is tapped, the map delegate will receive the event and is handled with

optional func mapView(_ mapView: MKMapView, 
       annotationView view: MKAnnotationView, 
       calloutAccessoryControlTapped control: UIControl)