如何使用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
  }

我怎么解决这个问题,如果注释和用户的位置相同,它将不仅显示用户当前的位置,而且还会显示注释?

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

问题很简单。我以某种方式忘记了制作mapView.delegate = self。现在一切都很好!

本文链接:https://www.f2er.com/2718813.html

大家都在问