map annotation with different marker using swift 3

柔情痞子 提交于 2019-12-11 08:48:20

问题


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
}

回答1:


Same thing I Have Done with Custom MKPointAnnotation Class

class MyPointAnnotation : MKPointAnnotation {
   var obj : ComparableData?

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

Setup Map

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 Delegate Methods

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
}


来源:https://stackoverflow.com/questions/51944487/map-annotation-with-different-marker-using-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!