0

I've tow UIViewControllers the first one displays an MKMapKitView with annotations and the user's current location and in the second viewController I'll be constructing the annotations that will be displayed to the first viewController.

I found out that if the user's location and the annotation are the same the mapView will only display the user's current location.

Here's my code to create an annotation:

  func addAnnotiation(for locationCoordinate: CLLocationCoordinate2D) {
    let annotationPoint = MKPointAnnotation()
    annotationPoint.coordinate = locationCoordinate
    mapView.addAnnotation(annotationPoint)
  }

My MKMapViewDelegate method:

  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let identifier = "Annotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.canShowCallout = true
    } else {
        annotationView!.annotation = annotation
    }

    return annotationView
  }

How can I solve this problem so if the annotation and the user's location are the same it will show the annotation also and not only the user's current location?

Hassan ElDesouky
  • 334
  • 4
  • 17
  • if i am reading this correct your want to add, annotation pin to same coordinates as users current location? – chirag90 Jan 24 '20 at 11:34
  • Yes, the annotation pins 'might' be in the same as the user's current location and if so... it should be displayed over the user's current location. – Hassan ElDesouky Jan 24 '20 at 11:44
  • 1
    Have a look at this [question](https://stackoverflow.com/q/40894722/4056108) – chirag90 Jan 24 '20 at 11:46

1 Answers1

0

The problem was very simple. I somehow forgot to make mapView.delegate = self. Everything is working just fine right now!

Hassan ElDesouky
  • 334
  • 4
  • 17