ios - (MapView) Userlocation image changed 帮助恢复

标签 ios swift mkmapview mkannotation

我正在使用 MapKit 并更改了注释的图像,但问题是,用户位置(蓝点)也更改了它的图片(我不希望这样)。我怎样才能恢复正常。此外,我在标注中添加了一个小按钮(正如您可能已经猜到的那样)已添加到我的位置并且它不应该在那里。我的代码如下...

    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,您不想做任何事情。

关于ios - (MapView) Userlocation image changed 帮助恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44211612/

相关文章:

ios - 如何获取我在 iPhone 上使用的 Siri 的 AVSpeechSynthesisVoice 标识符?或者我怎样才能让一个人正确地发音?

json - 如何在 swift xcode 中为多个 json 表创建结构

ios - 使用 iPhone 中的 map View 读取当前位置名称

ios - 数据txt文件在iPhone模拟器中读取时有效,但在iPhone设备上无效?

ios - 关闭 iPad 上的自动锁定 : Up-To-Date Information

ios - 类的实例和正确的初始化

ios - 添加 viewForAnnotation 时注释标题不出现

ios - 在 Swift 中格式化电话号码

ios - Xcode 是否支持任何形式的 Assets 将一张图像缩放到所有 Assets 大小?

ios - 平移时跟踪 MKMapView centerCoordinate