Reputation: 3685
I have the below code to set the pin image based which object from an array was used to create the pin. The issue is that the pins are not showing the right image for the pin.
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
}
these pins are created with:
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)
}
}
The out put of the print suggestions it should be working right, how ever the wrong images are still on the wrong pins.
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
Upvotes: 0
Views: 204
Reputation: 535944
Your image-choosing logic is faulty (it's in the wrong place). You need to perform your image-choosing logic for every annotation that arrives, not just the new ones that are not reused.
This is what you are doing:
if annotationView == nil {
// your image-choosing logic
} else {
annotationView?.annotation = annotation
}
So if your annotation view is a reused annotation, you do nothing; your logic is never used. But that means you don't determine what image goes here. But there is an image: the image from the old, reused annotation view. Thus, it is the wrong image for this annotation.
Upvotes: 1