我有下面的代码来设置图钉图像,基于数组中的哪个对象用于创建图钉。 问题是图钉未显示图钉的正确图像。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
if annotation is MKUserLocation {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
var selectedStation = fuel()
for station in fuelStations {
if (annotation.title! == station.Name){
selectedStation = station
}
}
var imageName = ""
switch(selectedStation.Brand) {
case "MORRISONS" :
imageName = "fuel-morrisons"
break;
case "TEXACO" :
imageName = "fuel-texaco"
break;
case "SAINSBURY" :
imageName = "fuel-sainsbury"
break;
case "SHELL" :
imageName = "fuel-shell"
break;
case "BP" :
imageName = "fuel-bp"
break;
case "TESCO" :
imageName = "fuel-tesco"
break;
case "TESCO EXTRA" :
imageName = "fuel-tesco"
break;
case "ESSO" :
imageName = "fuel-esso"
break;
default : imageName = "NEWFuel"
}
print("selected station: " + selectedStation.Brand + " imagename: " + imageName)
let pinImage = UIImage(named: imageName)
let size = CGSize(width: 40, height: 45)
UIGraphicsBeginImageContext(size)
pinImage!.draw(in: CGRect(x:0, y:0, width:size.width, height:size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
annotationView?.image = resizedImage
} else {
annotationView?.annotation = annotation
}
return annotationView
}
这些图钉是用:
for station in self.fuelStations{
let myAnnotation: MKPointAnnotation = MKPointAnnotation()
myAnnotation.coordinate = CLLocationCoordinate2DMake(station.lat, station.lng);
myAnnotation.title = station.Name
myAnnotation.subtitle = station.Brand
DispatchQueue.main.async {
self.mapView.addAnnotation(myAnnotation)
}
}
打印的输出表明它应该工作正常,但错误的图像仍然在错误的针脚上。
selected station: TOTAL imagename: NEWFuel
selected station: TESCO EXTRA imagename: fuel-tesco
selected station: BP imagename: fuel-bp
selected station: ESSO imagename: fuel-esso
selected station: GULF imagename: NEWFuel
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: TESCO EXTRA imagename: fuel-tesco
selected station: MURCO imagename: NEWFuel
selected station: BP imagename: fuel-bp
selected station: GULF imagename: NEWFuel
selected station: JET imagename: NEWFuel
selected station: ESSO imagename: fuel-esso
selected station: GULF imagename: NEWFuel
selected station: ASDA imagename: NEWFuel
selected station: BP imagename: fuel-bp
selected station: TESCO imagename: fuel-tesco
selected station: JET imagename: NEWFuel
selected station: SHELL imagename: fuel-shell
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: SHELL imagename: fuel-shell
selected station: ASDA imagename: NEWFuel
selected station: SHELL imagename: fuel-shell
selected station: BP imagename: fuel-bp
selected station: ESSO imagename: fuel-esso
selected station: ESSO imagename: fuel-esso
selected station: TOTAL imagename: NEWFuel
最佳答案
您的图片选择逻辑有问题(位置错误)。您需要为到达的每个 注释执行图像选择逻辑,而不仅仅是未重复使用的新注释。
这就是你正在做的:
if annotationView == nil {
// your image-choosing logic
} else {
annotationView?.annotation = annotation
}
因此,如果您的注释 View 是一个重用 注释,您什么都不做;你的逻辑从未被使用过。但这意味着您无法确定此处放的是什么图像。但是有一张图片:来自旧的、重复使用的注释 View 的图片。因此,该注释的图像是错误的。
关于ios - 为显示错误图像的 MKMapView 设置 annotationView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47611848/