Repository navigation
Add bearing to markers - #8836
Conversation
|
Can you say more about the use case for this? I'm specifically curious why setting a marker bearing should force it to be flat on the surface of the map. |
|
So the use case is for situations where we want whatever image we use to lay flat on the map, and potentially want it to move around (i.e. a location marker, updating with GPS). The method until now has been to create another layer with the image and move it, but that method has been shown to be sluggish and is overkill in most cases. This keeps the marker as a div and simplifies that functionality. |
|
Have you thought about keeping this consistent with The latter would unlock #8034. |
Yeah, this is what I was about to propose as well. I don't think the pitch of the marker needs to be tied to the bearing. I like the general idea here though. |
|
Here's the original issue #3937 for |
|
Writing this I did feel like the pitch and the bearing could be added separately, but with both available it provides the functionality I need. I will update this to be consistent with the functionality in |
|
A core piece of this is to have precise control of the direction the marker is pointing. It doesn't look like a |
https://docs.mapbox.com/mapbox-gl-js/example/animate-point-along-route/ shows |
|
Yep, I think it's reasonable to implement |
|
Mind you the specific combination of: , I can't see the use for, but all the other combinations I can understand and I think would be great for Marker to support. You can tweak the example at https://docs.mapbox.com/mapbox-gl-js/example/add-image/ to see the affects of changing those properties for Symbol. |
|
Made the updates, the options are now |
|
@dburnsii I tested this out and it's awesome! Supporting rotationAlignment and pitchAlignment makes this much more flexible, opening up further use cases, whist being consistent with Just a couple more things I think this PR would need to address:
|
|
Thanks for the unit tests, apart from the index.html which should either be part of the debug pages or removed, this PR is great! |
There was a problem hiding this comment.
I updated the markers debug page to test this out edbac23 and it looks great! I'll follow up with another PR with the debug page change.

in this example North is pointing to the right, and the markers have rotation: 0
I think this is good to merge as it stands.
A potential change or option to rotate around the offset can be done in a future issue/pr.
ryanhamley
left a comment
There was a problem hiding this comment.
Overall, this looks really good! I have a request for changing one aspect of behavior and then I think we can be more explicit and careful in our descriptions of the properties, methods and parameters.
| * @param {boolean} [options.draggable=false] A boolean indicating whether or not a marker is able to be dragged to a new position on the map. | ||
| * @param {number} [options.rotation=0] The rotation angle of the marker (clockwise, in degrees), relative to its respective {@link Marker#rotationAlignment} setting. | ||
| * @param {string} [options.pitchAlignment='auto'] `map` aligns the `Marker`'s pitch relative to the map, `viewport` aligns the `Marker`'s pitch relative to the viewport. | ||
| * @param {string} [options.rotationAlignment='auto'] `map` aligns the `Marker`'s rotation relative to the map, `viewport` aligns the `Marker`'s rotation relative to the viewport. |
There was a problem hiding this comment.
I think we should explicitly note that by default, pitchAlignment and rotationAlignment are set to auto which in the case of rotationAlignment is equivalent to viewport. But by analogy with icon-rotation-alignment and icon-pitch-alignment, we can see that if icon-pitch-alignment is set to auto, it matches whatever icon-rotation-alignment is set to. I think it would be good to match that behavior with these options.
There was a problem hiding this comment.
I also think that we could more closely align the wording of the descriptions here with what we have already for the symbol style properties. So for pitchAlignment, something like "map aligns the Marker to the plane of the map. viewport aligns the Marker to the plane of the viewport`." We also should use proper punctuation in the comments.
cc @chloekraw for thoughts on wording (you may want to review the wording for the function descriptions starting on line 515 as well)
There was a problem hiding this comment.
I used your wording for pitchAlignment, but there are extra properties that make the icon-rotation-alignmentdescription as easy to drop in. Changed that to:
"map aligns the Marker's rotation relative to the map, maintaining a bearing as the map rotates. viewport aligns the Marker's rotation relative to the viewport, agnostic to map rotations. auto is equivalent to viewport."
Thoughts?
| this._draggable = options && options.draggable || false; | ||
| this._state = 'inactive'; | ||
| this._rotation = options && options.rotation || 0; | ||
| this._pitchAlignment = options && options.pitchAlignment || 'auto'; |
There was a problem hiding this comment.
Again, I think if options.pitchAlignment is auto, this._pitchAlignment should match whatever options.rotationAlignment is.
There was a problem hiding this comment.
Set to options && options.pitchAlignment || this._rotationAlignment, putting it after this._rotationAlignment is set.
| } | ||
|
|
||
| /** | ||
| * Sets the `rotation` property for pointing the marker in a particular direction |
There was a problem hiding this comment.
I think the wording on this comment isn't quite right. If the marker is set relative to the viewport, then it's not really correct to say it's pointing in a particular "direction". There's also no indication in the description of the method or the rotation parameter that rotation is in degrees or what positive and negative degrees would do (clockwise vs counter-clockwise).
There was a problem hiding this comment.
Agreed this should be changed, but it goes back to if we should repeat the documentation for the property, or omit duplicating it and expect people to refer back to the rotation property documentation.
I think less duplication is better as you don't need to re-read the same documentation in multiple places and ensure it's consistent.
* @param {number} [options.rotation=0] The rotation angle of the marker (clockwise, in degrees), relative to its respective {@link Marker#rotationAlignment} setting.
There was a problem hiding this comment.
Updated to "Sets the rotationAlignment of the marker."
| * @param {PointLike} [options.offset] The offset in pixels as a {@link PointLike} object to apply relative to the element's center. Negatives indicate left and up. | ||
| * @param {string} [options.color='#3FB1CE'] The color to use for the default marker if options.element is not provided. The default is light blue. | ||
| * @param {boolean} [options.draggable=false] A boolean indicating whether or not a marker is able to be dragged to a new position on the map. | ||
| * @param {number} [options.rotation=0] The rotation angle of the marker (clockwise, in degrees), relative to its respective {@link Marker#rotationAlignment} setting. |
There was a problem hiding this comment.
As far as I can tell, nothing in this PR restricts rotation to the clockwise direction (which I think is good!). If you set rotation = -45, then the marker will rotate 45 degrees counterclockwise, right? So we should spell that out here.
There was a problem hiding this comment.
Maybe just enough to replace "clockwise, in degrees" with "a positive rotation angle is clockwise, units in degrees"?
There was a problem hiding this comment.
Maybe The rotation angle of the marker in degrees, relative to its respective {@link Marker#rotationAlignment} setting. A positive value will rotate the marker clockwise.
There was a problem hiding this comment.
Updated the parameter description.
| } | ||
|
|
||
| /** | ||
| * Returns the current rotation angle |
| } | ||
|
|
||
| /** | ||
| * Sets the `rotateAlignment` property for pointing the marker in a particular direction |
There was a problem hiding this comment.
reference to direction here as well. I'm not sure what the best way to say this is, but I think this isn't quite right. @andrewharvey thoughts?
There was a problem hiding this comment.
I would just use "Sets the rotationAlignment of the marker." Further documentation of exactly what rotationAlignment is is further up in the options and I don't think needs to be duplicated here? At least this is what other options like draggable/setDraggable do. Open to other opinions though.
There was a problem hiding this comment.
I like that approach and I totally missed that this needs to be rotationAlignment
There was a problem hiding this comment.
Used that wording for setPitchAlignment and setRotationAlignment, and fixed the rotateAlignment typo.
| * @returns {Marker} `this` | ||
| */ | ||
| setPitchAlignment(alignment: string) { | ||
| this._pitchAlignment = alignment || 'auto'; |
There was a problem hiding this comment.
This would also need to change to match rotationAlignment when alignment is auto
There was a problem hiding this comment.
Updated to alignment || this._rotationAlignment
| this._state = 'inactive'; | ||
| this._rotation = options && options.rotation || 0; | ||
| this._rotationAlignment = options && options.rotationAlignment || 'auto'; | ||
| this._pitchAlignment = options && options.pitchAlignment || this._rotationAlignment; |
There was a problem hiding this comment.
This wouldn't match pitchAlignment to rotationAlignment if pitchAlignment = 'auto' and rotationAlignment = 'map'. This should be options && options.pitchAlignment && options.pitchAlignment === 'auto' ? this._rotationAlignment : options.pitchAlignment
| * @returns {Marker} `this` | ||
| */ | ||
| setPitchAlignment(alignment: string) { | ||
| this._pitchAlignment = alignment || this._rotationAlignment; |
There was a problem hiding this comment.
I think this should also be alignment === 'auto' ? this._rotationAlignment : alignment.
There was a problem hiding this comment.
Did this:
this._pitchAlignment = alignment && alignment !== 'auto' ? alignment : this._rotationAlignment;
To also check if it's undefined.
|
|
||
| /** | ||
| * Sets the `pitchAlignment` property of the marker. | ||
| * @param {string} [alignment='auto'] Sets the `pitchAlignment` property of the marker. |
There was a problem hiding this comment.
This doesn't actually default to auto. I think we should remove the default here and make a note that if alignment is 'auto', it will automatically match rotationAlignment.
There was a problem hiding this comment.
Changed the wording on that.
| t.end(); | ||
| }); | ||
|
|
||
| test('Marker can set rotationAlignment and pitchAlignment', (t) => { |
There was a problem hiding this comment.
We should probably add a test that ensures setting rotationAlignment === 'map' and pitchAlignment === 'auto' results in pitchAlignment === 'map'. Both through the options object and by calling set{Rotation/Pitch}Alignment
There was a problem hiding this comment.
Added a couple tests. One is for the option on initialization, the other is for the getter/setters.
Description:
This PR adds a parameter to a Marker that enables a bearing to be set. This keeps the marker flat on the map surface, and allowing the marker to point in any direction. This replaces a previous workaround that involved creating an additional layer on top of an existing layer.
Before/After:




Changes to API
New option for marker:
bearingNew functions for marker:
marker.getBearing()&marker.setBearing(bearing)closes #3937