Last active
May 22, 2020 15:40
-
-
Save robertmryan/1a250ec09d4e868643fd5470f548d06a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
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
FWIW, this is what results: