From 8ae3bdbfe83d92c47712094ed82aae17e2b7f634 Mon Sep 17 00:00:00 2001 From: Taro Matsuzawa Date: Sat, 18 May 2024 15:27:37 +0900 Subject: [PATCH] #3103 added showButton option to GeoLocate control --- src/ui/control/geolocate_control.test.ts | 9 +++++++++ src/ui/control/geolocate_control.ts | 13 ++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ui/control/geolocate_control.test.ts b/src/ui/control/geolocate_control.test.ts index 49d518bb7c..ba96315573 100644 --- a/src/ui/control/geolocate_control.test.ts +++ b/src/ui/control/geolocate_control.test.ts @@ -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'); + }); }); diff --git a/src/ui/control/geolocate_control.ts b/src/ui/control/geolocate_control.ts index 7c1194298d..ac21786b73 100644 --- a/src/ui/control/geolocate_control.ts +++ b/src/ui/control/geolocate_control.ts @@ -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 = { @@ -51,7 +56,8 @@ const defaultOptions: GeolocateControlOptions = { }, trackUserLocation: false, showAccuracyCircle: true, - showUserLocation: true + showUserLocation: true, + showButton: true }; let numberOfWatches = 0; @@ -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');