【发布时间】: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