使用Swift 3使用不同的标记来标注地图 [英] map annotation with different marker using swift 3

查看:108
本文介绍了使用Swift 3使用不同的标记来标注地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用地图视图注释. 标记注释应使用停车规则类型显示 如果已支付,则支付"大头针图像,如果是免费",则免费大头针图像 我将所有注释都显示为付费"图片

I am working in map view annotation. The marker annotation should be displayed using the parking rule type If paid pin image be "paid" and if free pin image be "free" I am getting all annotation as "paid" image

我在下面附加了我的代码,在此问题上,有谁能帮助我解决

I have attached my code below can any one help me in this issue to fix

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    // Don't want to show a custom image if the annotation is the user's location.
    guard !(annotation is MKUserLocation) else {
        return nil
    }

    // Better to make this class property
    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    }

    if let annotationView = annotationView {
        // Configure your annotation view here
        if parkingTypeArray.count > 0 {


            for cameraa in parkingTypeArray.enumerated() {

                if cameraa.element == "Free street parking" {

                    let pinImage = UIImage(named: "free")
                    annotationView.image = pinImage

                }else if cameraa.element == "Paid street parking" {

                    let pinImage = UIImage(named: "paid")
                    annotationView.image = pinImage

                }else if cameraa.element == "Paid parking" {

                    let pinImage = UIImage(named: "paid")
                    annotationView.image = pinImage
                }
            }
        }
    }

    return annotationView
}

推荐答案

我使用自定义功能所做的相同 MKPointAnnotation 课程

class MyPointAnnotation : MKPointAnnotation {
   var obj : ComparableData?

    init(data_obj : ComparableData) {
        self.obj = data_obj
        super.init()
    }
}

设置地图

for item in self.Arr_Map_Comparables{

    if item.Latitude != "" && item.Longitude != ""{
        let annotation = MyPointAnnotation(data_obj: item)
        annotation.coordinate = CLLocationCoordinate2D(latitude: Double(item.Latitude!)!, longitude: Double(item.Longitude!)!)
        annotation.title = item.Full_Address
        mapView.addAnnotation(annotation)
    }

}    
self.focusMarkers(markers: mapView.annotations, width: 50)

MapView委托方法

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
    // Don't want to show a custom image if the annotation is the user's location.
    guard !(annotation is MKUserLocation) else {
        return nil
    }

    // Better to make this class property
    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    }

    if let annotationView = annotationView {
        // Configure your annotation view here
        annotationView.canShowCallout = true

        if let annotation = annotationView.annotation as? MyPointAnnotation{

            if annotation.obj?.Status_Mls == "Active"{

                annotationView.image = UIImage(named: "active")

            }else if annotation.obj?.Status_Mls == "Sold"{

                annotationView.image = UIImage(named: "sold")

            }else{
                annotationView.image = UIImage(named: "other")
            }
        }
    }
    return annotationView
}

这篇关于使用Swift 3使用不同的标记来标注地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆