I created an app which displays an MKMapView and displays 6 annotations in the Greenwood Indiana area. I tested it on my phone. When I selected one of the annotations, I was expecting the "didSelect annotation" and the "did Select annotation view" functions to both execute and display a message on the console, but I only got the "didSelect annotation view" message. It appears the "didSelect annotation" function did not execute when the annotation was selected.
My Xcode IOS minimum deployment is 15.6 I added an entry in the plist for "private - location when in usage description" My current location is Greenwood Indiana
My code is:
import UIKit import MapKit import CoreLocation
class PlaceAnnotation: MKPointAnnotation { var id: String
init (id: String) {
self.id = id
}
}
class Memorial { var name: String var coordinates: CLLocationCoordinate2D var address: String?
init (name: String, coordinates: CLLocationCoordinate2D, address: String) {
self.name = name
self.coordinates = coordinates
self.address = address
}
}
class MapViewController: UIViewController {
var locationManager = CLLocationManager()
lazy var mapView: MKMapView = {
let mapView = MKMapView()
return mapView
}()
var regionInMeters: Double = 10000
var zoomInMeters: Double = 10000
var locationArray: [Memorial] = []
override func viewDidLoad() {
super.viewDidLoad()
style()
buildDummyInput()
buildConstraints()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
checkLocationServices()
}
func buildDummyInput() {
let lat1 = 39.612982
let lon1 = -86.109473
let point2D1 = CLLocationCoordinate2D(latitude: lat1, longitude: lon1)
let address1 = "300 S Madison Ave, Greenwood, IN 46142, United States"
var location1 = Memorial(name: "Able Adams", coordinates: point2D1, address: address1)
let lat2 = 39.60590
let lon2 = -86.09034
let point2D2 = CLLocationCoordinate2D(latitude: lat2, longitude: lon2)
let address2 = "Smith Valley Rd, Greenwood, IN 46143, United States"
var location2 = Memorial(name: "Betty Btevents", coordinates: point2D2, address: address2)
let lat3 = 39.60732
let lon3 = -86.08367
let point2D3 = CLLocationCoordinate2D(latitude: lat3, longitude: lon3)
let address3 = "Smith Valley Rd, Greenwood, IN 46143, United States"
var location3 = Memorial(name: "Chuck Cummings", coordinates: point2D3, address: address3)
let lat4 = 39.62826
let lon4 = -86.12178
let point2D4 = CLLocationCoordinate2D(latitude: lat4, longitude: lon4)
let address4 = "Fry Rd, Greenwood, IN 46142, United States"
var location4 = Memorial(name: "Dawn Denny", coordinates: point2D4, address: address4)
let lat5 = 39.63544
let lon5 = -86.12657
let point2D5 = CLLocationCoordinate2D(latitude: lat5, longitude: lon5)
let address5 = "2524 E County Line Rd, Indianapolis, IN 46227 United States"
var location5 = Memorial(name: "Fred Flintstone", coordinates: point2D5, address: address5)
let lat6 = 39.63594
let lon6 = -86.11790
let point2D6 = CLLocationCoordinate2D(latitude: lat6, longitude: lon6)
let address6 = "E County Line Rd, Greenwood, IN 46142, United States"
var location6 = Memorial(name: "Gale Globbins", coordinates: point2D6, address: address6)
locationArray.removeAll()
locationArray.append(location1)
locationArray.append(location2)
locationArray.append(location3)
locationArray.append(location4)
locationArray.append(location5)
locationArray.append(location6)
}
func style() {
mapView.delegate = self
mapView.showsUserLocation = true
view.addSubview(mapView)
}
func buildConstraints() {
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
mapView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
mapView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
mapView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
}
func checkLocationServices() {
DispatchQueue.global().async {
if CLLocationManager.locationServicesEnabled() {
self.setUpLocationManager()
self.checkLocationAuthorization()
} else {
print ("Location service not available")
}
}
}
func setUpLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func checkLocationAuthorization() {
switch locationManager.authorizationStatus {
case .authorizedAlways:
break
case .authorizedWhenInUse:
findUserLocation()
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .denied:
print ("location services status = denied")
case .restricted:
print ("location services status = restricted")
@unknown default:
print ("location services status = unknown")
}
}
func findUserLocation() {
centerViewOnUserLocation()
displayAnnotations()
}
func centerViewOnUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
}
func displayAnnotations() {
mapView.removeAnnotations(mapView.annotations)
for annotation in locationArray {
let holdUUID = UUID().uuidString.lowercased()
let holdAnnotation = PlaceAnnotation(id: holdUUID)
holdAnnotation.title = annotation.name
holdAnnotation.subtitle = annotation.address
holdAnnotation.coordinate = CLLocationCoordinate2D(latitude: annotation.coordinates.latitude, longitude: annotation.coordinates.longitude)
mapView.addAnnotation(holdAnnotation)
}
}
}
extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didSelect annotation: MKAnnotation) {
print ("did select annotation function started")
// do more stuff
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print ("did select annotation view function started")
}
}
extension MapViewController: CLLocationManagerDelegate { func locationManager ( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { }
func locationManager ( _ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
checkLocationAuthorization()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print (error)
}
}
I expected two messages to display on the console when I selected an annotation on a map. I expected a message which said "did select annotation function started" and a message which said "did select annotation view function started". When I selected an annotation I only got the "did select annotation function view started" message.