mathias merlot
mathias merlot

Reputation: 595

how to add a pin with image when gesture on map

I would like to add a pin on the Map with an image on a gesture. Actually the add pin on gesture feature works great but without the image because it has to be a MKAnnotationView and not just MKAnnotation. So I got weird warnings that I'm unable to fix: Image of warning

Upvotes: 0

Views: 62

Answers (2)

Roy Bernal
Roy Bernal

Reputation: 121

Maybe the errors are because MKPinAnnotationView always use a pin image, so you can't be override. You should use MKAnnotationView instead.

Upvotes: 0

gunas
gunas

Reputation: 1907

        override func viewDidLoad() {
            super.viewDidLoad()
            let annotation = MKPointAnnotation()
            annotation.title = location.title
            annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
            mapView.addAnnotation(annotation)
    }



func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation { return nil }

        let identifier = "CustomAnnotation"

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = false
            annotationView?.backgroundColor = UIColor.clear
            annotationView!.image = UIImage(named: "map-pinpoint.png")!
        } else {
            annotationView!.annotation = annotation
        }
        return annotationView
    }

Upvotes: 1

Related Questions