2017-01-14 23 views
1

私は奇妙な振る舞いで私の頭を壊していました。私はMOSA10のMKMapViewにMKPointAnnotationを表示しようとしたときに気づいています。私が直面している問題に答える。iOS swift:MKPointAnnotationは常にタイトルを表示していません

  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には少なくとも1つの文字が必要です""さもなければ、.titleが後で ""以外に変更されても、ラベルは表示されません。

私の状況:私は、ユーザーが地図上で長押しすると注釈が追加される機能があります。タイトルは基本的に、CLGeocoder()。reverseGeocodeLocation(...)ルックアップを使用して検出されたアドレスの最初の利用可能な行です。すべてが失敗した場合、単に日付を使用します。注釈は、タイトルにテキストがあることが絶対に確実なときに、最後に追加されるだけです。これは、上記の2番目の投稿からのコメントをこの状況に適合させません。

私の問題:上部には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) 
    } 
} 

誰でも私に論理を表示することができ、どのようにこれが起こっているのか、素晴らしいことでしょう。

答えて

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) 
    } 
} 
関連する問題