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

问题描述 投票:0回答:1

我拖了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
  }

我该如何解决此问题,所以如果注释和用户的位置相同,它将显示注释,而不仅是用户当前的位置?

ios swift mapkit mkmapview mapkitannotation
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.