iOS swift:MKPointAnnotation并不总是显示标题

时间:2017-01-14 19:23:03

标签: ios swift mkmapview mkpointannotation

在尝试在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必须至少有一个字符,即使这是一个" &#34 ;.否则,即使.title随后被修改为""以外,标签也不会显示。'
  3. 我的情况:我有一个功能,当用户在地图上长按时,会添加注释。标题基本上是使用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)
        }
    }
    

    如果有人能告诉我逻辑以及如何/为何发生这种情况,那就太棒了。

1 个答案:

答案 0 :(得分:0)

您需要更改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)
    }
}