在地图上手势时如何在图像上添加图钉

时间:2018-08-09 10:07:03

标签: swift

我想在地图上添加图钉并添加手势图像。 实际上,手势功能上的添加别针效果很好,但是没有图像,因为它必须是MKAnnotationView,而不仅仅是MKAnnotation。 因此,我收到了无法解决的奇怪警告:Image of warning

2 个答案:

答案 0 :(得分:1)

        override func viewDidLoad() {
            super.viewDidLoad()
            let annotation = MKPointAnnotation()
            annotation.title = location.title
            annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
            mapView.addAnnotation(annotation)
    }



func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation { return nil }

        let identifier = "CustomAnnotation"

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = false
            annotationView?.backgroundColor = UIColor.clear
            annotationView!.image = UIImage(named: "map-pinpoint.png")!
        } else {
            annotationView!.annotation = annotation
        }
        return annotationView
    }

答案 1 :(得分:0)

错误可能是因为MKPinAnnotationView始终使用固定图像,所以您无法覆盖它。您应该改用MKAnnotationView。

相关问题