【问题标题】:iOS swift: MKPointAnnotation not always showing titleiOS swift:MKPointAnnotation 并不总是显示标题
【发布时间】:2017-01-14 19:23:02
【问题描述】:

我在尝试在 iOS 10 中的 MKMapView 上显示 MKPointAnnotation 时注意到的一个奇怪行为让我头疼不已。我在 StackOverflow 上找到了 2 篇相关帖子,但都没有真正回答我面临的问题。它们是:

  1. https://stackoverflow.com/questions/36760810/mkmapview-annotation-title-is-not-showing 但这没有任何答案,而且问题似乎略有不同,因为这里的标题从不显示,而在我的情况下它确实显示但有一个奇怪的解决方法。
  2. https://stackoverflow.com/questions/33818285/ios-mapview-annotations-not-showing-for-pins 但用户只是忘记实际设置标题。尽管@rockhammer 在最后有一个有趣的评论,但它有点相关但不完全是:“似乎在将注释添加到地图时,.title 必须至少有一个字符,即使它是“”。否则,即使 .title 随后被修改为非 "",标签也不会显示。'

我的情况:我有一个功能,当用户长按地图时,将添加注释。标题基本上是使用 CLGeocoder().reverseGeocodeLocation(...) 查找找到的地址的第一个可用行。如果一切都失败了,它将简单地使用日期。您会注意到,只有在绝对确定标题中有文本时,才会一直添加注释。这使得上面提到的第二篇文章的评论不适合这种情况。

我的问题:您会注意到顶部附近的行annotation.title = "BLAH"。如果没有这一行,注释标题将不会显示在 MKMapView 中,而只会显示引脚!

@IBOutlet weak var map: MKMapView!

func longPress(gestureRecognizer: UIGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizerState.began {
        let touchPoint = gestureRecognizer.location(in: self.map)
        let coordinate = map.convert(touchPoint, toCoordinateFrom: self.map)
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = "BLAH" //WITHOUT THIS THE TITLE NEVER SHOWS

        CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) { (placemarks, error) in
            if error != nil {
                print(error!)
            } else {
                if let placemark = placemarks?[0] {
                    annotation.title = placemark.subThoroughfare != nil ? placemark.subThoroughfare! : ""
                    annotation.title = annotation.title! + (annotation.title! == "" ? "" : " ") + (placemark.thoroughfare != nil ? placemark.thoroughfare! : "")
                    if annotation.title == "" {
                        annotation.title = placemark.subLocality != nil ? placemark.subLocality! : ""
                        if annotation.title == "" {
                            annotation.title = placemark.subAdministrativeArea != nil ? placemark.subAdministrativeArea! : ""
                            if annotation.title == "" {
                                annotation.title = placemark.postalCode != nil ? placemark.postalCode! : ""
                                if annotation.title == "" {
                                    annotation.title = placemark.country != nil ? placemark.country! : ""
                                }
                            }
                        }
                    }
                }
            }
            if annotation.title == "" {
                annotation.title = "Added \(NSDate())"
            } //At this point a title is guaranteed to be set. Still, it never shows unless it is first 'initialised' with 'BLAH' or something at the beginning.
        }

        self.map.addAnnotation(annotation)
    }
}

如果有人可以向我展示这种情况的逻辑以及发生的方式/原因,那就太好了。

【问题讨论】:

    标签: ios swift mkmapview mkpointannotation


    【解决方案1】:

    您需要更改3个不正确的方法; 我不知道为什么这种方法不起作用。

    我向您推荐其他一些编译和工作的方法,通常,很好。你需要:

    • 将UIGestureRecognizerState.began改为UIGestureRecognizerState.Began;

    • 将touchPoint = gestureRecognizer.location(in: self.map)改为touchPoint = gestureRecognizer.locationInView(self.mapView);

    • 最后,将coordinate = mapView.convert(teste, toCoordinateFrom: self.mapView)更改为coordinate = mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)。

    最后你的代码应该是这样的:

    @IBOutlet weak var map: MKMapView!
    
    
    func longPress(gestureRecognizer: UIGestureRecognizer) {
        if gestureRecognizer.state == UIGestureRecognizerState.Began {     // And not .began
            let touchPoint: CGPoint = gestureRecognizer.locationInView(self.map)   //And not location(in: self.map)
        //let coordinate = mapView.convert(teste, toCoordinateFrom: self.map)
        let coordinate = map.convertPoint(touchPoint, toCoordinateFromView: self.map)
            let annotation = MKPointAnnotation()
            annotation.coordinate = coordinate
            //annotation.title = "BLAH" //WITHOUT THIS THE TITLE NEVER SHOWS
    
            CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) { (placemarks, error) in
                if error != nil {
                    print(error!)
                } else {
                    if let placemark = placemarks?[0] {
                        annotation.title = placemark.subThoroughfare != nil ? placemark.subThoroughfare! : ""
                        annotation.title = annotation.title! + (annotation.title! == "" ? "" : " ") + (placemark.thoroughfare != nil ? placemark.thoroughfare! : "")
                        if annotation.title == "" {
                            annotation.title = placemark.subLocality != nil ? placemark.subLocality! : ""
                            if annotation.title == "" {
                                annotation.title = placemark.subAdministrativeArea != nil ? placemark.subAdministrativeArea! : ""
                                if annotation.title == "" {
                                    annotation.title = placemark.postalCode != nil ? placemark.postalCode! : ""
                                    if annotation.title == "" {
                                        annotation.title = placemark.country != nil ? placemark.country! : ""
                                    }
                                }
                            }
                        }
                    }
                }
    
                if annotation.title == "" {
                    annotation.title = "Added \(NSDate())"
                } //At this point a title is guaranteed to be set. Still, it never shows unless it is first 'initialised' with 'BLAH' or something at the beginning.
            }
    
            self.map.addAnnotation(annotation)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多