This what I'm trying to do:
Use a single custom annotation for all the locations that will populate the map. I have the custom image saved in the assets but, can't make it work.
My code is as follows:
ViewDidLoad()
if let locationDict = snap.value as? Dictionary<String, AnyObject> {
let lat = locationDict["LATITUDE"] as! CLLocationDegrees
let long = locationDict["LONGITUDE"] as! CLLocationDegrees
let title = locationDict["NAME"] as! String
let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
_ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.10, longitudeDelta: 0.10))
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
annotation.title = title.capitalized // if you need title, add this line
self.mapView.addAnnotation(annotation)
}
Use a single custom annotation for all the locations that will populate the map. I have the custom image saved in the assets but, can't make it work (Details in the viewDidLoad)
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView: MKAnnotationView?
if annotation.isKind(of: MKUserLocation.self) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
annotationView?.image = UIImage(named: "icon")
}
return annotationView
}
You should implement delegate method:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
annotationView.image = UIImage(named: "icon")
return annotationView
}
let reuseId = "Image"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
annotationView?.canShowCallout = true
annotationView?.image = UIImage(named: "<<image name>>")
}
else {
annotationView?.annotation = annotation
}
return annotationView
}