Reputation: 145
I would like to show annotations, based on a specific list, with the green color. How can I do it? Here is my code:
for i in closestpharmacyname{
var docref2 = db.collection("pharmacies").document(i)
print("Pharmacy: ", i)
docref2.getDocument(source: .cache) { (document, error) in
if var document = document {
var pharmacylatitude = document.get("latitude") as! Double
var pharmacylongitude = document.get("longitude") as! Double
print(pharmacylatitude, pharmacylongitude)
var pharmacyannotation = MKPointAnnotation()
pharmacyannotation.coordinate = CLLocationCoordinate2D(latitude: pharmacylatitude, longitude: pharmacylongitude)
pharmacyannotation.title = i
self.MapView.addAnnotation(pharmacyannotation)
}else{
print("Document does not exist")
}
}
}
Everything works, except that the annotations are the standard red color.
Upvotes: 1
Views: 3188
Reputation: 326
Try using MKMarkerAnnotationView instead, so you can customize the color. As far as I know it's not possible to change the color of an MKPointAnnotation. MKMarkerAnnotationView looks the same, too.
More specifically, you need to add this function, which from my understanding takes the MKPointAnnotation and returns a MKMarkerAnnotationView, with you being able to customize each one, meaning you can use switch statements to give each annotation a different color conditionally.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "something")
annotationView.markerTintColor = .green //custom colors also work, additionally to these default ones
return annotationView
}
For a better understanding on customizing annotations, including custom symbols in pictures instead of the default, check out this link: https://medium.com/better-programming/how-to-customize-mapkit-annotations-baad32487a7
Upvotes: 6