设置注解MKMapView显示错误的图像
设置注释查看图像我有以下代码来设置引脚图像基于阵列中的哪个对象被用来创建引脚。 问题是引脚没有显示引脚的正确图像。设置注解MKMapView显示错误的图像
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
}
所以,如果您的注解视图是一个重用注释,你什么都不做;你的逻辑从未被使用过。但这意味着你不能确定这里的图像。但有一个图像:来自旧的重用注释视图的图像。因此,这个注释是错误的图像。
以上是 设置注解MKMapView显示错误的图像 的全部内容, 来源链接: utcz.com/qa/258021.html