自定义注释显示所有不同类型的POI的相同图像

自定义注释显示所有不同类型的POI的相同图像

本文介绍了自定义注释显示所有不同类型的POI的相同图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在地图上为不同的cat_ID显示不同的注释.在这个问题上,我只包括两个类别.

I need to show different annotation for different cat_ID on map. in this question i am including only two categories .

    let item = json["data"].arrayValue
    var count = 0
    initMK()
    dispatch_async(dispatch_get_main_queue()) {
    while count < item.count{
        let lat = item[count]["comp_lat"].stringValue
        let lng = item[count]["comp_lng"].stringValue

        if item[count]["ct_id"].stringValue == "783"{
            self.state = "Jachthavens"
            let poi = MKPointAnnotation()
            let coordinates = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(lng)!)
            poi.coordinate = coordinates
            poi.title = item[count]["comp_name_pub"].stringValue
            poi.subtitle = "Jachthavens"
            //poi.id
            self.mapView.addAnnotation(poi)
            self.mapView.reloadInputViews()


        }else if item[count]["ct_id"].stringValue == "788"{

            self.state = "Watersportwinkels"
            let poi = MKPointAnnotation()
            let coordinates = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(lng)!)
            poi.coordinate = coordinates
            poi.title = item[count]["comp_name_pub"].stringValue
            poi.subtitle = "Watersportwinkels"
            self.mapView.addAnnotation(poi)
            self.mapView.reloadInputViews()
            }
            count = count + 1
            }
         }

//////////这是我设置不同图像的viewForAnnotation方法,仅当(state =="Watersportwinkels")和(state =="Jachthavens")时出现.结果,我在地图上只能看到"Jachthavens"图像,但信息窗口中的详细信息是正确的.

///////// This is my viewForAnnotation method for setting different images problem comes in only if (state == "Watersportwinkels") and (state == "Jachthavens"). In result i can see only "Jachthavens" Image on my map but details on info window is correct .

让标识符="MyPin"

let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)

    // Reuse the annotation if possible
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

    if annotationView == nil
    {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView!.canShowCallout = true
        if(state == "track")
        {
        annotationView!.image = UIImage(named: "startpin")
        }
        else if(state == "pause")
        {
        annotationView!.image = UIImage(named: "pausepin")
        }
        else if(state == "stop")
        {
         annotationView!.image = UIImage(named: "endpin")
        }else if (state == "Jachthavens") {
            annotationView!.image = UIImage(named: "jachthavenpin")
                let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
                deleteButton.frame.size.width = 35
                deleteButton.frame.size.height = 35
                deleteButton.backgroundColor = UIColor.whiteColor()
                deleteButton.setImage(UIImage(named: "jachthaven"), forState: .Normal)
                deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
                annotationView!.leftCalloutAccessoryView = deleteButton
            annotationView?.annotation = annotation



        }else if (state == "Watersportwinkels"){
            annotationView!.image = UIImage(named: "watersportwinkelspin")
            let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
            deleteButton.frame.size.width = 35
            deleteButton.frame.size.height = 35
            deleteButton.backgroundColor = UIColor.whiteColor()
            deleteButton.setImage(UIImage(named: "watersportwinkels"), forState: .Normal)
            deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
            annotationView!.leftCalloutAccessoryView = deleteButton
            annotationView?.annotation = annotation
        }

    }
    else
    {
        annotationView!.annotation = annotation
    }

    return annotationView

}

推荐答案

您可以尝试更改:

}else if  {

进入:

else if(state == "Jachthavens")

更新:

检查字幕而不是状态:

if annotationView == nil {

    annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
    annotationView!.canShowCallout = true

    if(annotationView.subtitle == "track") {
        annotationView!.image = UIImage(named: "startpin")
    } else if(annotationView.subtitle == "pause") {
        annotationView!.image = UIImage(named: "pausepin")
    } else if(annotationView.subtitle == "stop") {
        annotationView!.image = UIImage(named: "endpin")
    } else if (annotationView.subtitle == "Jachthavens") {
        annotationView!.image = UIImage(named: "jachthavenpin")
        let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
        deleteButton.frame.size.width = 35
        deleteButton.frame.size.height = 35
        deleteButton.backgroundColor = UIColor.whiteColor()
        deleteButton.setImage(UIImage(named: "jachthaven"), forState: .Normal)
        deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
        annotationView!.leftCalloutAccessoryView = deleteButton
        annotationView?.annotation = annotation
    } else if (annotationView.subtitle == "Watersportwinkels") {
        annotationView!.image = UIImage(named: "watersportwinkelspin")
        let deleteButton = UIButton(type: UIButtonType.Custom) as UIButton
        deleteButton.frame.size.width = 35
        deleteButton.frame.size.height = 35
        deleteButton.backgroundColor = UIColor.whiteColor()
        deleteButton.setImage(UIImage(named: "watersportwinkels"), forState: .Normal)
        deleteButton.addTarget(self, action: #selector(ViewController.infoClicked(_:)), forControlEvents: .TouchUpInside)
        annotationView!.leftCalloutAccessoryView = deleteButton
        annotationView?.annotation = annotation
    }

} else {
    annotationView!.annotation = annotation
}

这篇关于自定义注释显示所有不同类型的POI的相同图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:50