如何解决Swift:自定义注释丢失字幕
我创建了一个自定义注释,然后将其放置在地图上。然后,当我选择它时,它不再具有字幕属性值,并且失去了info属性。
自定义注释:
import Foundation
import MapKit
class GAPoint: NSObject,MKAnnotation {
var title: String?
var subTitle: String?
var coordinate: CLLocationCoordinate2D
var info: String
init(title: String,subTitle: String,coordinate: CLLocationCoordinate2D,info: String) {
self.title = title
self.subTitle = subTitle
self.coordinate = coordinate
self.info = info
}
}
将其放在地图上:
var titles = ["title1","title2"]
var ids = ["id1","id2"]
for i in 0...1 {
// Create Annotation Array
let point = CLLocationCoordinate2D(latitude:
fences[i].CENTER_LAT,longitude: fences[i].CENTER_LONG)
//Now use this point to add annotation on map.
let title = titles[i]
let id = ids[i]
let annotation = GAPoint(title: title,subTitle: id,coordinate: point,info: id)
annotations.append(annotation)
}
// Add Annotation to map
self.mapView?.addAnnotations(annotations)
在添加注释时调用:
// Annotation View Properties (Pin Added or Moved) ---------------------
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
print("mv viewFor Annotation: Pin Added or Moved")
// If Annotation (Pin) is Not GAPoint Don't Add
guard annotation is GAPoint else { return nil }
if let identifier = annotation.title {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier!)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: identifier)
//annotationView?.canShowCallout = true
annotationView?.isDraggable = true
// Create Label
let annotationLabel = UILabel(frame: CGRect(x: -75,y: -8,width: 150,height: 20))
annotationLabel.numberOfLines = 1
annotationLabel.textAlignment = .center
annotationLabel.font = UIFont(name: "Arial",size: 10)
annotationLabel.text = identifier
annotationLabel.backgroundColor = UIColor.white
annotationLabel.layer.cornerRadius = 10
annotationLabel.clipsToBounds = true
annotationView?.addSubview(annotationLabel)
//annotationView?.image = UIImage(named: "gaPin.png")
} else {
annotationView?.annotation = annotation
}
return annotationView
} else {
return nil
}
}
在选择注释时调用: (这是缺少字幕值的地方 并且info属性已消失)
func mapView(_ mapView: MKMapView,didSelect view: MKAnnotationView) {
print("mv didSelect view: Selected a pin")
guard view.annotation is GAPoint else { return }
if let annotation = view.annotation {
if let title1 = annotation.title {
if let title = title1 {
print(title)
}
}
if let stitle1 = annotation.subtitle {
if let stitle = stitle1 {
print(stitle)
}
}
}
}
解决方法
我做了一些研究,然后尝试使用 以下代码:
let loc : GAPoint = annotation as! GAPoint
在委托人的上下文中:
func mapView(_ mapView: MKMapView,didSelect view: MKAnnotationView) {
print("mv didSelect view: Selected a pin")
guard view.annotation is GAPoint else { return }
if let annotation = view.annotation {
let loc : GAPoint = annotation as! GAPoint
if let title1 = annotation.title {
if let title = title1 {
print(title)
}
}
if let stitle1 = annotation.subtitle {
if let stitle = stitle1 {
print(stitle)
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。