Skip to content

Commit

Permalink
Add getter and setter for offset on marker
Browse files Browse the repository at this point in the history
  • Loading branch information
bartvde authored and jfirebaugh committed Nov 28, 2017
1 parent d837781 commit d69688b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ui/marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ class Marker {

DOM.setTransform(this._element, `translate(-50%, -50%) translate(${this._pos.x}px, ${this._pos.y}px)`);
}

/**
* Get the marker's offset.
* @returns {Point}
*/
getOffset() {
return this._offset;
}

/**
* Sets the offset of the marker
* @param {PointLike} offset The offset in pixels as a {@link PointLike} object to apply relative to the element's center. Negatives indicate left and up.
* @returns {Marker} `this`
*/
setOffset(offset: PointLike) {
this._offset = Point.convert(offset);
this._update();
return this;
}
}

module.exports = Marker;
11 changes: 11 additions & 0 deletions test/unit/ui/marker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,16 @@ test('Marker', (t) => {
t.end();
});

t.test('marker\'s offset can be changed', (t) => {
const map = createMap();
const marker = new Marker(window.document.createElement('div')).setLngLat([-77.01866, 38.888]).addTo(map);
const offset = marker.getOffset();
t.ok(offset.x === 0 && offset.y === 0, 'default offset');
t.ok(marker.setOffset([50, -75]) instanceof Marker, 'marker.setOffset() returns Marker instance');
const newOffset = marker.getOffset();
t.ok(newOffset.x === 50 && newOffset.y === -75, 'marker\'s offset can be updated');
t.end();
});

t.end();
});

0 comments on commit d69688b

Please sign in to comment.