【问题标题】:map annotation with different marker using swift 3使用 swift 3 使用不同的标记映射注释
【发布时间】:2018-08-21 08:20:20
【问题描述】:

我正在研究地图视图注释。 标记注释应使用停车规则类型显示 如果付费的 pin 图像是“付费的”,如果免费的 pin 图像是“免费的” 我将所有注释都作为“付费”图像

我在下面附上了我的代码,任何人都可以帮助我解决这个问题

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
}

【问题讨论】:

  • 因为“camerra”的for循环。您的最后一个元素将是“付费街边停车”,以便将所有图片设置为付费。
  • @SaurabhPrajapati 那么它只需要最后一个元素吗?如何解决此问题
  • 请显示此代码的更多上下文。 cameraa是什么类?
  • @KosukeOgawa parkTypeArray 有“免费路边停车”、“付费路边停车”、“付费停车”三个值。如果免费 pin 图像应显示为免费图像标记,如果付费意味着图像应显示为免费图像标记。而cameraa是fo循环
  • 最近我回答了类似的问题。 stackoverflow.com/questions/51889111/…

标签: ios swift mkmapview mkannotationview


【解决方案1】:

我对自定义做了同样的事情 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多