-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose mapView(:ViewForAnnotation:) #498
Conversation
Examples/Swift/ViewController.swift
Outdated
@@ -242,6 +242,34 @@ class ViewController: UIViewController, MGLMapViewDelegate { | |||
present(navigationViewController, animated: true, completion: nil) | |||
} | |||
|
|||
func navigationMapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? { | |||
guard annotation is MGLUserLocation else { return nil } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, I'm never getting an annotation with type MGLUserLocation
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this has to do with some of the user location overriding that the navigation SDK does…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason is that MGLMapView.showsUserLocation
is set during storyboard initialization, causing MGLMapView to call MGLMapViewDelegate.mapView(_:viewFor:)
. Unfortunately, navigationMapView
isn’t set until much later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in b35dfe1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed mapbox/mapbox-gl-native#9781 to address the root cause.
Examples/Swift/ViewController.swift
Outdated
@@ -242,6 +242,34 @@ class ViewController: UIViewController, MGLMapViewDelegate { | |||
present(navigationViewController, animated: true, completion: nil) | |||
} | |||
|
|||
func navigationMapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should be implemented in the extension below that conforms to NavigationViewControllerDelegate.
@@ -101,6 +101,8 @@ public protocol NavigationViewControllerDelegate { | |||
If this method is unimplemented, the navigation map view will represent the destination annotation with the default marker. | |||
*/ | |||
@objc optional func navigationMapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? | |||
|
|||
@objc optional func navigationMapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method needs documentation.
Examples/Swift/ViewController.swift
Outdated
@@ -242,6 +242,34 @@ class ViewController: UIViewController, MGLMapViewDelegate { | |||
present(navigationViewController, animated: true, completion: nil) | |||
} | |||
|
|||
func navigationMapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? { | |||
guard annotation is MGLUserLocation else { return nil } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this has to do with some of the user location overriding that the navigation SDK does…
This PR would fix #213. |
showsUserLocation preinitializes the user location annotation view very early on, but the delegate has yet to be set by that time, let alone the navigation map delegate. When each of the delegate properties is set, force the map view to reconsider everything it does as part of showing the user location.
*/ | ||
func refreshShowsUserLocation() { | ||
showsUserLocation = false | ||
showsUserLocation = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will result in -[MGLMapView updateUserLocationAnnotationViewAnimatedWithDuration:]
getting called a lot more frequently than before when setting up the map view. From a cursory glance, I don’t think it’ll be a problem in practice.
/** | ||
Returns a view object to mark the given point annotation object on the map. | ||
|
||
The user location annotation view can also be customized via this method. When annotation is an instance of `MGLUserLocation` (or equal to the map view’s userLocation property), return an instance of `MGLUserLocationAnnotationView` (or a subclass thereof). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or equal to the map view’s userLocation property
The developer doesn’t have direct access to MGLMapView.userLocation
, do they?
@1ec5 updated. |
This exposes the ability to provide a view for any annotation on the map. In the example, I show how to provide a view for the user puck.
/cc @1ec5 @frederoni @ericrwolfe