(MapView)用户位置图像更改帮助恢复
问题描述:
我正在使用MapKit,我更改了Annotation的图像,但问题是,用户位置(蓝点)也改变了它的图片(我不希望那样)。我怎样才能恢复正常。另外,我在标注中添加了一个小按钮(正如您可能已经猜到的那样),该按钮已添加到我的位置,但它不应该在那里。我的代码如下...(MapView)用户位置图像更改帮助恢复
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKUserLocation) {
print("annotation is MKUserLocation")
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")
if annotationView == nil{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
annotationView!.canShowCallout = true
annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
let pinImg = UIImage(named: "curz")
let size = CGSize(width: 50, height: 50)
UIGraphicsBeginImageContext(size)
pinImg!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
annotationView!.image = resizedImage
}
else {
annotationView!.annotation = annotation
}
return annotationView
}
//Var SelectedAnnotation
var anotacionSeleccionada : MKPointAnnotation!
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
anotacionSeleccionada = view.annotation as? MKPointAnnotation
performSegue(withIdentifier: "vista", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ViewController {
destination.pin = anotacionSeleccionada
}
}
答
在你viewFor annotation
函数开头添加此行:
if annotation is MKUserLocation {
return nil
}
因为不想将做任何事情,如果it's的MKUserLocation
。
我是否将该行代替“if!(注解是MKUserLocation)”? –
是的,你可以@DanielC。 –
它的工作原理!谢谢! –