Your code shown is inconsistent and cannot be compiled.
Maybe you have mixed up some different versions of your code.
I assume `Location` as:
//Locationvariables
struct Location {
let title: String
let latitude: Double
let longitude: Double
let timeA: Float //<-
let timeB: Float //<-
}
And I needed to fill some whitespaces.
With those fixes above, I can see two annotations with custom image.
So, I do not understand why shows all the annotations but with the normal apple-logo/ without my custom image.
You may have another version of your code and might be watching the result of a different code.
Anyway, since you have shown a non-buildable code, I cannot rely on the behavior you have described.
Though, the image assigned is not as expected, so I just show you how to fix that part.
With your code (I mean the code shown in your post with my fixes), you use the global variable `icon`.
So, in your `mapView(_:viewFor:)` uses the last value assigned to `icon`, which may be all the same.
You use many global variables, but as far as I can see, any of them have no reason to be global.
So, first of all, your custom annotation should have its own `icon` as it may change in each instance.
//create custom Annotation
class CustomAnnotation: MKPointAnnotation {
var isCollapsed = true
var setNeedsToggle = false
var icon: String = ""
}
(By the way, don't you feel creepy using non-Capitalized name for types?)
And then remove all global variables and update your for-in loop.
override func viewDidLoad() {
super.viewDidLoad()
//userLocation
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
//Timevariables
let date = Date()
let calendar = Calendar.current
let minutes = calendar.component(.minute, from: date)
let hour = calendar.component(.hour, from: date)
let Hour = hour*100 + minutes
var icon = ""
//Loop creating different Locations
for location in locations {
let annotation = CustomAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D (latitude: location.latitude, longitude: location.longitude)
let timeAInt = Int(location.timeA)
let timeBInt = Int(location.timeB)
if timeAInt ... timeBInt ~= Hour { icon = "testPin1" }
else { icon = "testPin2" }
annotation.icon = icon //<-
map.addAnnotation(annotation)
}
self.map.delegate = self
}
You also need to update your `mapView(_:viewFor:)` in order to use the `icon` property of the CustomAnnotation.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annot = annotation as? CustomAnnotation else { return nil }
let annotationView = MKAnnotationView(annotation: annot, reuseIdentifier: "customAnnotation")
annotationView.image = UIImage(named: annot.icon)
annotationView.canShowCallout = true
let rightButton = UIButton(type: .detailDisclosure)
rightButton.setImage(UIImage(named: annot.isCollapsed ? "ic_showmore" : "ic_showless"), for: .normal)
annotationView.rightCalloutAccessoryView = rightButton
if annot.isCollapsed {
annotationView.detailCalloutAccessoryView = nil
} else {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.text = "Description Shown When Button is Tapped"
label.font = UIFont.italicSystemFont(ofSize: 14.0)
label.numberOfLines = 0
annotationView.detailCalloutAccessoryView = label
label.widthAnchor.constraint(lessThanOrEqualToConstant: label.frame.width).isActive = true
label.heightAnchor.constraint(lessThanOrEqualToConstant: 90.0).isActive = true
}
return annotationView
}
You may need to fix some other issues, but when you ask something more, please show your actual latest code which compiles.