地图上的自定义图钉

时间:2016-12-17 01:34:59

标签: ios swift mkmapview

我的问题是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
}

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

1 个答案:

答案 0 :(得分:1)

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

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
}

Purple pin

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
    }

Annotation with custom image

相关问题