【问题标题】:Update mapView pin colour based on boolean property根据布尔属性更新 mapView 引脚颜色
【发布时间】:2018-03-07 09:45:19
【问题描述】:

我正在尝试根据热点是否已访问来更新mapView 引脚的颜色。

请参阅下面的代码。下面是订阅 MKAnnotation 委托的热点类。确定引脚颜色的属性是访问的 Bool。

init(title: String, hotspotName: String,coordinate: CLLocationCoordinate2D, ARType:String, hotspotId:String)
{
    //assing values to properties
    self.hotspotTitle = title
    self.hotspotName = hotspotName
    self.ARType = ARType
    self.coordinate = coordinate
    self.hotspotId = hotspotId
    super.init()
}

var visited: Bool {
    return self.completed
}

var title: String?
{
    return hotspotTitle
}

var subtitle: String?
{

    return hotspotName
}

下面是我用来尝试更新引脚颜色的 mapKit 委托方法。

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

    // check if annotation is of type Hotspot, as we dont want to alter other types of annotations
    guard annotation is Hotspot else {
        return  nil
    }

    let identifier = "Hotspot"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    let pinView = MKPinAnnotationView(annotation:annotation,reuseIdentifier: identifier)
    if annotationView == nil
    {

        if let hotspot = annotationView?.annotation as? Hotspot {
            if !hotspot.visited
            {

                pinView.pinTintColor = UIColor(red:0.32, green:0.82, blue:0.4, alpha:1)
                pinView.tintColor = UIColor(white: 0.0, alpha: 0.5)
            }
            else
            {
                pinView.pinTintColor = UIColor(red:0.0, green:0.0, blue:0.90, alpha:1)
                pinView.tintColor = UIColor(white: 0.0, alpha: 0.5)


            }
        }


        pinView.frame = mapView.frame
        pinView.isEnabled = true
        pinView.canShowCallout = true
        pinView.animatesDrop = true
        let rightButton = UIButton(type: .detailDisclosure)
        rightButton.addTarget(self,action: #selector(getDirections),for: .touchUpInside)
        pinView.rightCalloutAccessoryView = rightButton
        annotationView = pinView

    }

    if let annotationView = annotationView
    {

        annotationView.annotation = annotation


        // 5
        let button = annotationView.rightCalloutAccessoryView as! UIButton
        if let index = trail.hotspots.index(of: annotation as! Hotspot)
        {

            button.tag = index
        }
    }

    return annotationView
}

【问题讨论】:

  • 尝试一个不太聪明的代码从 MKMapView 中删除所有注释视图并再次添加此代码是删除注释let allAnnotations = self.mapView.annotations self.mapView.removeAnnotations(allAnnotations) 并使用每次添加它们时使用的相同代码注释更改的属性访问另一种方法是循环所有注释视图并仅删除坐标与值更改的注释相同的注释并再次添加它
  • 还要检查是否调用了您使用的方法而不是方法func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) { <#code#> }

标签: ios swift mapkit mapkitannotation


【解决方案1】:

if annotationView == nil 语句中更新mapView pin 颜色。

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

    // check if annotation is of type Hotspot, as we dont want to alter other types of annotations
    guard annotation is Hotspot else {
        return  nil
    }

    let identifier = "Hotspot"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    let pinView = MKPinAnnotationView(annotation:annotation,reuseIdentifier: identifier)
    if annotationView == nil
    {
        pinView.frame = mapView.frame
        pinView.isEnabled = true
        pinView.canShowCallout = true
        pinView.animatesDrop = true
        let rightButton = UIButton(type: .detailDisclosure)
        rightButton.addTarget(self,action: #selector(getDirections),for: .touchUpInside)
        pinView.rightCalloutAccessoryView = rightButton
        annotationView = pinView
    }

    if let hotspot = annotationView?.annotation as? Hotspot {
        if !hotspot.visited
        {
            annotationView.pinTintColor = UIColor(red:0.32, green:0.82, blue:0.4, alpha:1)
            annotationView.tintColor = UIColor(white: 0.0, alpha: 0.5)
        }
        else
        {
            annotationView.pinTintColor = UIColor(red:0.0, green:0.0, blue:0.90, alpha:1)
            annotationView.tintColor = UIColor(white: 0.0, alpha: 0.5)
        }
    }

    if let annotationView = annotationView
    {

        annotationView.annotation = annotation


        // 5
        let button = annotationView.rightCalloutAccessoryView as! UIButton
        if let index = trail.hotspots.index(of: annotation as! Hotspot)
        {

            button.tag = index
        }
    }

    return annotationView
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 2022-01-13
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多