私は奇妙な振る舞いで私の頭を壊していました。私はMOSA10のMKMapViewにMKPointAnnotationを表示しようとしたときに気づいています。私が直面している問題に答える。iOS swift:MKPointAnnotationは常にタイトルを表示していません
- https://stackoverflow.com/questions/36760810/mkmapview-annotation-title-is-not-showingが、これはどんな答えを持っていない、と私の場合はそれがショーを行いますが、奇妙な回避策としながら、タイトルが表示されることはありませんここでのような問題は少し異なるようだ:彼らはあります。
- 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)
}
}
誰でも私に論理を表示することができ、どのようにこれが起こっているのか、素晴らしいことでしょう。