微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

迅速-Mapkit在if语句中更改pinTintColor

如何解决迅速-Mapkit在if语句中更改pinTintColor

我正在尝试根据变量更改针脚颜色-某些针脚的颜色会发生变化,但似乎是随机的,并且与我期望的颜色无关。我怀疑这与dequeueReusableAnnotationView有关。我尝试了没有但我得到了相同的结果。有任何想法吗 ?谢谢

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard annotation is MKPointAnnotation else { return nil }

     let identifier = "Annotation"
     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        let pin = MKPinAnnotationView(annotation: annotation,reuseIdentifier: identifier)

        pin.canShowCallout = true
        annotationView = pin

        if globalDataSet[counter].fields.numdocksavailable == 0 {

            print (counter,globalDataSet[counter].fields.numdocksavailable,globalDataSet[counter].fields.name)
            print ("pin.pinTintColor = UIColor.systemBlue")
            pin.pinTintColor = UIColor.systemBlue
            

        } else {
            print (counter,globalDataSet[counter].fields.name)
            print ("pin.pinTintColor = UIColor.systemRed")
            pin.pinTintColor = UIColor.systemRed
            
        }
        
        counter += 1

    } else {

        annotationView!.annotation = annotation
        
    }

    return annotationView
}

解决方法

尝试以下代码。

if annotationView == nil {
    let pin = MKPinAnnotationView(annotation: annotation,reuseIdentifier: identifier)

    pin.canShowCallout = true
    annotationView = pin
    

} else {

    annotationView!.annotation = annotation
    
}

if globalDataSet[counter].fields.numdocksavailable == 0 {

    print (counter,globalDataSet[counter].fields.numdocksavailable,globalDataSet[counter].fields.name)
    print ("annotationView.pinTintColor = UIColor.systemBlue")
    annotationView.pinTintColor = UIColor.systemBlue
        

} else {
    print (counter,globalDataSet[counter].fields.name)
    print ("annotationView.pinTintColor = UIColor.systemRed")
    annotationView.pinTintColor = UIColor.systemRed
        
}

counter += 1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。