Skip to content

Add bearing to markers - #8836

Merged
ryanhamley merged 20 commits into
mapbox:masterfrom
dburnsii:add-bearing-to-markers
Oct 29, 2019
Merged

ryanhamley merged 20 commits into
mapbox:masterfrom
dburnsii:add-bearing-to-markers

Conversation

@dburnsii

@dburnsii dburnsii commented Oct 4, 2019 •

Copy link
Copy Markdown
Contributor

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:
SpinningMarkerTop
ChangingPitch
Rotating
Panning

Changes to API
New option for marker: bearing

New functions for marker: marker.getBearing() & marker.setBearing(bearing)

closes #3937

@ryanhamley

Copy link
Copy Markdown
Contributor

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.

@dburnsii

dburnsii commented Oct 4, 2019

Copy link
Copy Markdown
Contributor Author

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.

@andrewharvey

Copy link
Copy Markdown
Collaborator

Have you thought about keeping this consistent with symbol, having separate rotation-alignment and pitch-alignment properties?

The latter would unlock #8034.

@ryanhamley

Copy link
Copy Markdown
Contributor

Have you thought about keeping this consistent with symbol, having separate rotation-alignment and pitch-alignment properties?

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.

@andrewharvey

andrewharvey commented Oct 4, 2019 •

Copy link
Copy Markdown
Collaborator

Here's the original issue #3937 for rotation-alignment for Marker.

@dburnsii

dburnsii commented Oct 4, 2019

Copy link
Copy Markdown
Contributor Author

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 symbol.

@dburnsii

dburnsii commented Oct 4, 2019

Copy link
Copy Markdown
Contributor Author

A core piece of this is to have precise control of the direction the marker is pointing. It doesn't look like a symbol has the ability to actually define the bearing. What would be a good way to include this while still being compatible with the symbol functionality?

@andrewharvey

Copy link
Copy Markdown
Collaborator

A core piece of this is to have precise control of the direction the marker is pointing. It doesn't look like a symbol has the ability to actually define the bearing.

https://docs.mapbox.com/mapbox-gl-js/example/animate-point-along-route/ shows icon-rotate.

@ryanhamley

Copy link
Copy Markdown
Contributor

Yep, I think it's reasonable to implement marker-rotate, marker-rotation-alignment and marker-pitch-alignment

@andrewharvey

Copy link
Copy Markdown
Collaborator

Mind you the specific combination of:

"icon-rotation-alignment": 'map',
"icon-pitch-alignment": 'viewport'

, 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.

@dburnsii

dburnsii commented Oct 7, 2019

Copy link
Copy Markdown
Contributor Author

Made the updates, the options are now rotate, rotationAlignment, and pitchAlignment when creating a marker, and setters and getters modify these values after object creation.

Comment thread src/ui/marker.js Resolved
Comment thread src/ui/marker.js Resolved
Comment thread src/ui/marker.js Resolved
@andrewharvey

andrewharvey commented Oct 10, 2019 •

Copy link
Copy Markdown
Collaborator

@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 Symbol.

Just a couple more things I think this PR would need to address:

  • unit tests
  • more documentation in the JSDoc documenting the map and viewport options. Although we're implementing these options the same as the symbol options from the Style Spec, they are independent so I think they should be documented on their own.

Comment thread src/ui/marker.js Resolved
Comment thread src/ui/marker.js Resolved
Comment thread src/ui/marker.js Resolved
Comment thread src/ui/marker.js Resolved
Comment thread index.html Resolved
@andrewharvey

Copy link
Copy Markdown
Collaborator

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!

@andrewharvey andrewharvey left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Screenshot from 2019-10-18 11-23-16
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 ryanhamley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/ui/marker.js Outdated
* @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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Oct 22, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread src/ui/marker.js Outdated
this._draggable = options && options.draggable || false;
this._state = 'inactive';
this._rotation = options && options.rotation || 0;
this._pitchAlignment = options && options.pitchAlignment || 'auto';

ghost Oct 22, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think if options.pitchAlignment is auto, this._pitchAlignment should match whatever options.rotationAlignment is.

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set to options && options.pitchAlignment || this._rotationAlignment, putting it after this._rotationAlignment is set.

Comment thread src/ui/marker.js Outdated
}

/**
* Sets the `rotation` property for pointing the marker in a particular direction

ghost Oct 22, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

ghost Oct 23, 2019

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to "Sets the rotationAlignment of the marker."

Comment thread src/ui/marker.js Outdated
* @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.

ghost Oct 22, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Oct 23, 2019

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just enough to replace "clockwise, in degrees" with "a positive rotation angle is clockwise, units in degrees"?

ghost Oct 23, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the parameter description.

Comment thread src/ui/marker.js Outdated
}

/**
* Returns the current rotation angle

ghost Oct 22, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in degrees

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread src/ui/marker.js Outdated
}

/**
* Sets the `rotateAlignment` property for pointing the marker in a particular direction

ghost Oct 22, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

ghost Oct 23, 2019

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Oct 23, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that approach and I totally missed that this needs to be rotationAlignment

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used that wording for setPitchAlignment and setRotationAlignment, and fixed the rotateAlignment typo.

Comment thread src/ui/marker.js Outdated
* @returns {Marker} `this`
*/
setPitchAlignment(alignment: string) {
this._pitchAlignment = alignment || 'auto';

ghost Oct 22, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also need to change to match rotationAlignment when alignment is auto

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to alignment || this._rotationAlignment

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes! I think the code to match pitchAlignment === 'auto' to rotationAlignment isn't quite right. If you can fix that and add a test ensuring that behavior works as expected, then this is good to go.

Comment thread src/ui/marker.js Outdated
this._state = 'inactive';
this._rotation = options && options.rotation || 0;
this._rotationAlignment = options && options.rotationAlignment || 'auto';
this._pitchAlignment = options && options.pitchAlignment || this._rotationAlignment;

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ghost Oct 29, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread src/ui/marker.js Outdated
* @returns {Marker} `this`
*/
setPitchAlignment(alignment: string) {
this._pitchAlignment = alignment || this._rotationAlignment;

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should also be alignment === 'auto' ? this._rotationAlignment : alignment.

ghost Oct 29, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this:

this._pitchAlignment = alignment && alignment !== 'auto' ? alignment : this._rotationAlignment;

To also check if it's undefined.

Comment thread src/ui/marker.js Outdated

/**
* Sets the `pitchAlignment` property of the marker.
* @param {string} [alignment='auto'] Sets the `pitchAlignment` property of the marker.

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

ghost Oct 29, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the wording on that.

t.end();
});

test('Marker can set rotationAlignment and pitchAlignment', (t) => {

ghost Oct 28, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

ghost Oct 29, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a couple tests. One is for the option on initialization, the other is for the getter/setters.

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sticking with this and getting it polished! And congrats on your first contribution to the project 🎉

@ryanhamley
ryanhamley merged commit 162a065 into mapbox:master Oct 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature request: adding {"rotation-alignment": "map"} option to mapboxgl.Marker()

4 participants