我有下面的代码来设置引脚图像,根据数组中的哪个对象来创建引脚。 问题是引脚没有显示引脚的正确图像。
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
答案 0 :(得分:1)
您的图像选择逻辑有问题(它位于错误的位置)。您需要为到达的每个注释执行图像选择逻辑,而不仅仅是未重复使用的新注释。
这就是你在做的事情:
if annotationView == nil {
// your image-choosing logic
} else {
annotationView?.annotation = annotation
}
因此,如果您的注释视图是重用注释,则不执行任何操作;你的逻辑从未使用过。但这意味着您无法确定此处的图像。但是有一个图像:来自旧的,重用的注释视图的图像。因此,这个注释是错误的图像。