如何显示自定义注释,即使它位于 Swift 的用户当前位置

How to show custom annotation even if it's on the user's current location with Swift

我拖了 UIViewControllers,第一个显示带有注释和用户当前位置的 MKMapKitView,第二个 viewController 我将构建将显示给第一个的注释 viewController.

我发现如果用户的位置和注释相同,mapView 将只显示用户的当前位置。

这是我创建注释的代码:

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

我的MKMapViewDelegate方法:

  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
  }

我该如何解决这个问题,如果注释和用户的位置相同,它也会显示注释,而不仅仅是用户的当前位置?

问题很简单。我不知何故忘记制作 mapView.delegate = self。现在一切正常!