Ios (MapView)用户位置图像已更改帮助还原
我正在使用MapKit,我更改了注释的图像,但问题是,Userlocation(蓝点)也更改了它的图片(我不希望这样)。我怎样才能使它恢复正常。此外,我在标注中添加了一个小按钮(您可能已经猜到),该按钮被添加到了我的位置,但它不应该在那里。我的代码如下Ios (MapView)用户位置图像已更改帮助还原,ios,swift,mkmapview,mkannotation,Ios,Swift,Mkmapview,Mkannotation,我正在使用MapKit,我更改了注释的图像,但问题是,Userlocation(蓝点)也更改了它的图片(我不希望这样)。我怎样才能使它恢复正常。此外,我在标注中添加了一个小按钮(您可能已经猜到),该按钮被添加到了我的位置,但它不应该在那里。我的代码如下 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if !(annotation
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
}
因为如果是
MKUserLocation
你不想做任何事情,我是否要把那行改为“if!(注释是MKUserLocation)”?是的,你可以@DanielC。只有一个问题,Userlocation不出现在模拟器中,而是在我在手机中使用它时出现,这正常吗?模拟器在使用位置时有时可能会出现错误,但正常情况下它应该工作。该功能为您设置注释视图(默认红色pin)dequeueReusableAnnotationView标识符
用于标识注释。