swift - 将注释链接到标识符 - Swift、MapKit
问题描述
所以,我创建了一个注释。注释出现在我的地图上,但我想更改它的视图。我不确定如何设置它的标识符。
如果您要回答这个问题,请在其中使用我的代码,而不是示例,因为我真的很困惑。
let annotation = MKPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
let span : MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-33.809335, 151.258695)
let region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
annotation.coordinate = location
annotation.title = "Bus Stop"
annotation.subtitle = "Street Name at Street Name"
mapView.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)
-> MKAnnotationView? {
let identifier = "marker"
var view: MKMarkerAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(
withIdentifier: identifier)
as? MKMarkerAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKMarkerAnnotationView(annotation: annotation,
reuseIdentifier: identifier)
view.markerTintColor = UIColor.blue
view.glyphImage = UIImage(named: "small-business-20")
view.selectedGlyphImage = UIImage(named: "small-business-40")
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
@IBOutlet weak var mapView: MKMapView!
解决方案
您可以使用mapView.view(for:)
方法来获取 MKMarkerAnnotationView。
@IBAction func hoge(_ sender: UIButton) {
if let ann = self.mapView.view(for: annotation) as? MKMarkerAnnotationView {
ann.glyphImage = UIImage(named: "Laugh")
}
}
推荐阅读
- python - 如何在python中重新采样和插入新数据
- dax - DAX 将每一行值除以累积和的结果
- node.js - 如何在 Node js 中处理并发 sftp 连接。出现错误没有可用的 sftp 连接
- c++ - 随机将字符转换为大写的函数
- c++ - 密码登录程序永远循环并显示不正确的结果
- git - 如何合并这些已停产的分支
- reactjs - 我需要类似于下面给出的引导响应导航栏组件的材料 UI 组件
- azure-devops - Azure Devops 分支选项卡未在构建管道中显示任何内容
- sql - 如何在 SQL 中创建 CREATE VIEW INVOICE_V?
- amazon-dynamodb - 根据用户角色/访问权限从 dynamodb 表中获取数据