迅速-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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...