Skip to content
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

feat: add isHighAccurate property to Marker #11136

Closed
wants to merge 10 commits into from
33 changes: 31 additions & 2 deletions src/ui/marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type Options = {
clickTolerance?: number,
rotation?: number,
rotationAlignment?: string,
pitchAlignment?: string
pitchAlignment?: string,
snapToPixel?: boolean
};

export const TERRAIN_OCCLUDED_OPACITY = 0.2;
Expand All @@ -44,6 +45,7 @@ export const TERRAIN_OCCLUDED_OPACITY = 0.2;
* @param {number} [options.rotation=0] The rotation angle of the marker in degrees, relative to its respective `rotationAlignment` setting. A positive value will rotate the marker clockwise.
* @param {string} [options.pitchAlignment='auto'] `map` aligns the `Marker` to the plane of the map. `viewport` aligns the `Marker` to the plane of the viewport. `auto` automatically matches the value of `rotationAlignment`.
* @param {string} [options.rotationAlignment='auto'] `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`.
* @param {boolean} [options.snapToPixel=true] A boolean indicating should `Marker` position round to pixel or not.
* @example
* // Create a new marker.
* const marker = new mapboxgl.Marker()
Expand Down Expand Up @@ -81,6 +83,7 @@ export default class Marker extends Evented {
_rotationAlignment: string;
_originalTabIndex: ?string; // original tabindex of _element
_fadeTimer: ?TimeoutID;
_snapToPixel: ?boolean;

constructor(options?: Options, legacyOptions?: Options) {
super();
Expand Down Expand Up @@ -110,6 +113,7 @@ export default class Marker extends Evented {
this._rotation = options && options.rotation || 0;
this._rotationAlignment = options && options.rotationAlignment || 'auto';
this._pitchAlignment = options && options.pitchAlignment && options.pitchAlignment !== 'auto' ? options.pitchAlignment : this._rotationAlignment;
this._snapToPixel = (options && options.snapToPixel) === undefined ? true : !!options.snapToPixel;

if (!options || !options.element) {
this._defaultMarker = true;
Expand Down Expand Up @@ -533,7 +537,7 @@ export default class Marker extends Evented {
// because rounding the coordinates at every `move` event causes stuttered zooming
// we only round them when _update is called with `moveend` or when its called with
// no arguments (when the Marker is initialized or Marker#setLngLat is invoked).
if (!e || e.type === "moveend") {
if (this._snapToPixel && (!e || e.type === "moveend")) {
SnailBones marked this conversation as resolved.
Show resolved Hide resolved
this._pos = this._pos.round();
}

Expand Down Expand Up @@ -791,4 +795,29 @@ export default class Marker extends Evented {
getPitchAlignment() {
return this._pitchAlignment;
}

/**
* Sets the `snapToPixel` property of the marker.
*
* @param {boolean} shouldSnapToPixel Enable or disable `snapToPixel`.
* @returns {Marker} Returns itself to allow for method chaining.
* @example
* marker.setSnapToPixel(true);
*/
setSnapToPixel(shouldSnapToPixel: boolean) {
this._snapToPixel = !!shouldSnapToPixel;
this._update();
return this;
Copy link
Contributor

Choose a reason for hiding this comment

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

We want to call this._update() after updating snapToPixel as in all the other setter methods. We should also handle the undefined condition here: this._snapToPixel = !!shouldSnapToPixel;.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ryanbaumann
I think calling _update when changing snapToPixel is redundant because _pos doesn't change so if call _update nothing changed. Am I right? let me know to remove calling _update on setSnapToPixel.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that setSnapToPixel(true) should update the position so that the snap can be immediately perceived by the user.

}

/**
* Returns the marker's current `snapToPixel` value.
*
* @returns {boolean} Returns the current `snapToPixel` property of the marker.
* @example
* const snapToPixel = marker.getSnapToPixel();
*/
getSnapToPixel() {
return this._snapToPixel;
}
}