user14279703
user14279703

Reputation:

how to change default image in MKUserLocation annotation in Swift iOS

I have a map view in my app that displays the user's location. when the user's location is tapped an image is displayed as below:

enter image description here

is there a way to make this image customized to my liking, so for instance I would like to show the photo that the user has in his own profile, or any other static image that I would like to add.

similar to how the apple maps would show your profile picture in your location dot.

how can I do that here?

here is my code:

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
       if annotation is MKUserLocation {
        print("user location image tapped")
        
           return nil
       }

       let reuseId = "pin"
       var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
       if pinView == nil {
           pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
           pinView?.animatesDrop = true
           pinView?.canShowCallout = true
           pinView?.isDraggable = true
        
         //  pinView?.pinColor = .purple

           let rightButton: AnyObject! = UIButton(type: UIButton.ButtonType.detailDisclosure)
           pinView?.rightCalloutAccessoryView = rightButton as? UIView
   
       }
       else {
           pinView?.annotation = annotation
       }
       
       return pinView
   }

any help on this? thank you.

Upvotes: 3

Views: 921

Answers (1)

roadRunner
roadRunner

Reputation: 201

let imageView = UIImageView(frame: CGRect(0, 0, 25, 25))
imageView.image = UIImage(named: "userpic");
imageView.layer.cornerRadius = imageView.layer.frame.size.width / 2
imageView.layer.masksToBounds = true
anotationView.frame = imageView.frame

Upvotes: -1

Related Questions