将注释链接到标识符-Swift、MapKit
因此,我创建了一个注释。注释显示在我的地图上,但我想更改它的视图。但我不知道如何设置它的标识符 如果你在回答这个问题,请使用我的代码,而不是一个例子,因为我真的很困惑将注释链接到标识符-Swift、MapKit,swift,mapkit,Swift,Mapkit,因此,我创建了一个注释。注释显示在我的地图上,但我想更改它的视图。但我不知道如何设置它的标识符 如果你在回答这个问题,请使用我的代码,而不是一个例子,因为我真的很困惑 let annotation = MKPointAnnotation() override func viewDidLoad() { super.viewDidLoad() let span : MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1) let
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.viewfor:方法获取mkMarkerNotationView
@IBAction func hoge(_ sender: UIButton) {
if let ann = self.mapView.view(for: annotation) as? MKMarkerAnnotationView {
ann.glyphImage = UIImage(named: "Laugh")
}
}
例如,您可以将其用作iAction。我编辑了我的答案,但似乎没有回答我的问题