0

The annotation is displayed in my map but I can't add a callout. I am planing to use the callout to change a label.text to the title of the annotation in the same ViewController.

I tried this for example but what am I missing to make it work.

I hope somebody can help me out since this problem I could solve everything with stackoverflow or youtube but I am trying for hours now :(

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {

        map.delegate = self

        let cor : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 50, longitude: 10)
        let region = MKCoordinateRegionMakeWithDistance(cor, 5000, 5000)
        self.map.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()
        annotation.coordinate.latitude = 50
        annotation.title = "test"
        annotation.subtitle = "hdhsadsa"
        annotation.coordinate.longitude = 10

        map.addAnnotation(annotation)
    }

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

        if annotation is MKUserLocation { return nil }

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier") as? MKPinAnnotationView

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
            annotationView?.canShowCallout = true
            annotationView?.rightCalloutAccessoryView = UIButton(type: .infoLight)
        } else {
            annotationView?.annotation = annotation
        }

        return annotationView
    }
}
vhristoskov
  • 2,074
  • 2
  • 16
  • 20
sarahaha
  • 95
  • 1
  • 8
  • Can you show the code where you have attempted to add the callout? Have you set your object as the map view's delegate? – Paulw11 Oct 11 '17 at 19:46
  • `viewForAnnotation` is an `MKMapViewDelegate` function; if you haven't set the object that implements that function as the map view's delegate then it won't be called. – Paulw11 Oct 11 '17 at 20:14
  • Thanks for your reply I added map.delegate = self or what do you mean and class FirstViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate – sarahaha Oct 11 '17 at 20:27
  • Yes, you need to do both of those things; declare that you class implements the protocol and add it as the map view's delegate. – Paulw11 Oct 11 '17 at 20:29
  • I did :( I imported MapKit, added MKMapViewDelegate, I made an output "map", set the map.delegate = self, added annotations and tried a code snippet like – sarahaha Oct 11 '17 at 20:35
  • func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { var view = mapView.dequeueReusableAnnotationView(withIdentifier:"annotationIdentifier") if view == nil { view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotationIdentifier") view?.canShowCallout = true view?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) } else { view?.annotation = annotation } return view } – sarahaha Oct 11 '17 at 20:35
  • Please [edit](https://stackoverflow.com/posts/46695448/edit) your question to show the relevant code you are using and describe what happens beyond "it doesn't work". Do your annotations appear? have you set a breakpoint in the delegate method? Does it get called? – Paulw11 Oct 11 '17 at 20:37
  • Ok I made a new project and just passed the importent code - of course that is just one of my many many attempts – sarahaha Oct 11 '17 at 21:09
  • actually it works now - thank you so much for your patience!!! The function changed to viewFor annotation – sarahaha Oct 11 '17 at 21:27

0 Answers0