【问题标题】:Custom pin on the map [duplicate]地图上的自定义图钉[重复]
【发布时间】:2016-12-17 01:34:58
【问题描述】:

我的问题是 annotationView.image = X 不起作用,我只是看不到特殊的 pin,如果我删除它只会变成紫色(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
}

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

【问题讨论】:

  • 问题是你使用的是MKPinAnnotationView。在旧的 iOS 版本中,您可以设置 image,但在最近的版本中不能。使用MKAnnotationView
  • 另外,如果您成功地将先前的注释视图出列,请记住在else 子句中将其annotation 设置为您的if 语句。

标签: ios swift mkmapview


【解决方案1】:

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

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

MKPinAnnotationView.image 被设置为一个引脚,你不能改变它。下面的代码将制作一个紫色图钉,其弹出窗口中有一张显示 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 属性设置为您想要的任何内容。也可以结合上面的代码添加右图

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
    }

【讨论】:

  • @Rob 第一条评论:同意。我错过了。编辑了我的答案。第二条评论:我认为 OP 可能想要一个紫色别针,所以第一个 sn-p 使用 MKPinAnnotationView。如答案所述,第二个snipper可以与第一个sn-p结合使用自定义图像和右图进行注释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
相关资源
最近更新 更多