Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active May 22, 2020 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/1a250ec09d4e868643fd5470f548d06a to your computer and use it in GitHub Desktop.
Save robertmryan/1a250ec09d4e868643fd5470f548d06a to your computer and use it in GitHub Desktop.
class DriverAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D // if this can change, I'd suggest making it `dynamic var`; if it can't change, I'd make it constant with `let`
var uid: String
init(uid: String, coordinate: CLLocationCoordinate2D) {
self.uid = uid
self.coordinate = coordinate
}
}
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
lazy var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestWhenInUseAuthorization()
}
// works if you add this
//
// mapView.register(MKPinAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
// works if you add this
//
// mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
// works fine if you do neither of those, too
let center = CLLocationCoordinate2DMake(37.332, -122.03071)
mapView.camera = MKMapCamera(lookingAtCenter: center, fromDistance: 1_000, pitch: 0, heading: 0)
let coordinate = CLLocationCoordinate2DMake(37.332693, -122.03071)
let annotation = DriverAnnotation(uid: UUID().uuidString, coordinate: coordinate)
mapView.addAnnotation(annotation)
mapView.userTrackingMode = .follow
}
}
@robertmryan
Copy link
Author

FWIW, this is what results:

Simulator Screen Shot - iPhone 11 Pro Max - 2020-05-20 at 10 34 46

@robertmryan
Copy link
Author

FWIW, it does appear that the displayPriority of the annotation view is defaulting to .defaultLow (although the documentation suggests it should default to .required.

So you might want to declare an annotation view class, that sets it to .required:

class CustomAnnotationView: MKMarkerAnnotationView {
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        displayPriority = .required
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? {
        didSet {
            displayPriority = .required
        }
    }
}

And

mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment