Skip to content

Commit

Permalink
maplibre#3103 added showButton option to GeoLocate control
Browse files Browse the repository at this point in the history
  • Loading branch information
smellman committed May 18, 2024
1 parent 753204c commit 8ae3bdb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/ui/control/geolocate_control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,13 @@ describe('GeolocateControl with no options', () => {
await zoomendPromise;
expect(geolocate._circleElement.style.width).toBeTruthy();
});

test('don\'t show button if showButton = false', async () => {
const geolocate = new GeolocateControl({
showButton: false
});
map.addControl(geolocate);
await sleep(0);
expect(geolocate._geolocateButton.style.display).toBe('none');
});
});
13 changes: 12 additions & 1 deletion src/ui/control/geolocate_control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type GeolocateControlOptions = {
* @defaultValue true
*/
showUserLocation?: boolean;
/**
* If `false` the Geolocate button will not be shown on the map.
* @defaultValue true
*/
showButton?: boolean;
};

const defaultOptions: GeolocateControlOptions = {
Expand All @@ -51,7 +56,8 @@ const defaultOptions: GeolocateControlOptions = {
},
trackUserLocation: false,
showAccuracyCircle: true,
showUserLocation: true
showUserLocation: true,
showButton: true
};

let numberOfWatches = 0;
Expand Down Expand Up @@ -488,6 +494,11 @@ export class GeolocateControl extends Evented implements IControl {
DOM.create('span', 'maplibregl-ctrl-icon', this._geolocateButton).setAttribute('aria-hidden', 'true');
this._geolocateButton.type = 'button';

// if showButton is false, don't add the button to the map
if (!this.options.showButton) {
this._geolocateButton.style.display = 'none';
}

if (supported === false) {
warnOnce('Geolocation support is not available so the GeolocateControl will be disabled.');
const title = this._map._getUIString('GeolocateControl.LocationNotAvailable');
Expand Down

0 comments on commit 8ae3bdb

Please sign in to comment.