Skip to content

Snap marker to pixel if it's lngLat is unchanged #13352

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/ui/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default class Marker extends Evented<MarkerEvents> {
_updateFrameId: number;
_updateMoving: () => void;
_occludedOpacity: number;
_lngLatDirty: boolean;

constructor(options?: MarkerOptions, legacyOptions?: MarkerOptions) {
super();
Expand Down Expand Up @@ -125,6 +126,7 @@ export default class Marker extends Evented<MarkerEvents> {
this._pitchAlignment = (options && options.pitchAlignment && options.pitchAlignment) || 'auto';
this._updateMoving = () => this._update(true);
this._occludedOpacity = (options && options.occludedOpacity) || 0.2;
this._lngLatDirty = false;

if (!options || !options.element) {
this._defaultMarker = true;
Expand Down Expand Up @@ -294,7 +296,9 @@ export default class Marker extends Evented<MarkerEvents> {
* @see [Example: Add a marker using a place name](https://docs.mapbox.com/mapbox-gl-js/example/marker-from-geocode/)
*/
setLngLat(lnglat: LngLatLike): this {
this._lngLat = LngLat.convert(lnglat);
const newLngLat = LngLat.convert(lnglat);
this._lngLatDirty = this._lngLat == null || this._lngLat.lat !== newLngLat.lat || this._lngLat.lng !== newLngLat.lng;
this._lngLat = newLngLat;
this._pos = null;
if (this._popup) this._popup.setLngLat(this._lngLat);
this._update(true);
Expand Down Expand Up @@ -575,8 +579,9 @@ export default class Marker extends Evented<MarkerEvents> {

// 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 (delaySnap === true) {
// no arguments (when the Marker is initialized or Marker#setLngLat is invoked) or
// when the coordinates have not changed.
if (delaySnap === true && this._lngLatDirty) {
this._updateFrameId = requestAnimationFrame(() => {
if (this._element && this._pos && this._anchor) {
this._pos = this._pos.round();
Expand All @@ -586,6 +591,7 @@ export default class Marker extends Evented<MarkerEvents> {
} else {
this._pos = this._pos.round();
}
this._lngLatDirty = false;

map._requestDomTask(() => {
if (!this._map) return;
Expand Down
10 changes: 10 additions & 0 deletions test/unit/ui/marker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,16 @@ describe('Snap To Pixel', () => {
}, 100);
});
});
test("Immediately Snap To Pixel After setLngLat if unchanged", async () => {
marker.setLngLat(marker.getLngLat());
const pos = marker._pos;
await new Promise(resolve => {
setTimeout(() => {
expect(marker._pos).toStrictEqual(pos.round());
resolve();
}, 100);
});
});
test("Immediately Snap To Pixel on moveend", () => {
map.fire(new Event("moveend"));
expect(marker._pos).toStrictEqual(marker._pos.round());
Expand Down