地图上的自定义图钉

Custom pin on the map

我的问题是 annotationView.image = X 不起作用,我只是看不到特殊的别针,如果我移除它,只有紫色 (pinTintColor) - 它变成红色..

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.delegate = self

    for mall in malls {
        let coords = CLLocation(latitude: mall.latitude, longitude: mall.longitude)
        let annotation = MyAnnotation.init(coordinate: coords.coordinate, mall: mall, title: mall.name, subtitle: mall.adress)

        self.mapView.addAnnotation(annotation)
    }
}

而且我无法将图像添加为附件视图。有什么问题?

 extension commonMapViewController: MKMapViewDelegate {

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

    let annotationIdentifier = "restAnnotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = rightImage
        annotationView?.canShowCallout = true

    }
    annotationView?.pinTintColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)

    annotationView?.image = UIImage(named: "malls.png")

    return annotationView
}

感谢您的帮助,请原谅我的愚蠢..

我不确定你想要实现什么,但我看到了 2 种可能性:

1。如果您想要在标注中带有右图的紫色图钉

MKPinAnnotationView.image设置为pin码,无法更改。下面的代码将制作一个紫色的图钉,其弹出窗口的右图显示 Apple 的总部。

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

    let annotationIdentifier = "restAnnotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MKPinAnnotationView

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.canShowCallout = true

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        imageView.image = #imageLiteral(resourceName: "rightImage")
        imageView.contentMode = .scaleAspectFit

        annotationView?.rightCalloutAccessoryView = imageView
    }

    annotationView?.annotation = annotation
    annotationView?.pinTintColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
    return annotationView
}

2。如果你想在地图上有一个图像

改用MKAnnotationView。您可以将其 image 属性 设置为您想要的任何值。也可以结合上面的代码添加一个right-image

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

        let annotationIdentifier = "restAnnotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView?.canShowCallout = true
        }

        annotationView?.annotation = annotation
        annotationView?.image = #imageLiteral(resourceName: "rightImageSmall")
        return annotationView
    }