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

#3103 added showButton option to GeoLocate control. #4134

Open
wants to merge 6 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## main

### ✨ Features and improvements

- Add `showButton` option to `GeoLocateControl`. ([#3103](https://github.com/maplibre/maplibre-gl-js/issues/3103))
- Add events to `GeolocateControl` to allow a more granular interaction ([#3847](https://github.com/maplibre/maplibre-gl-js/pull/3847))
- _...Add new stuff here..._

Expand Down
4 changes: 4 additions & 0 deletions src/css/maplibre-gl.css
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@
animation: maplibregl-spin 2s infinite linear;
}

.maplibregl-ctrl button.maplibregl-ctrl-geolocate.maplibregl-ctrl-geolocate-hidden {
display: none;
}

@media (-ms-high-contrast: active) {
.maplibregl-ctrl button.maplibregl-ctrl-geolocate .maplibregl-ctrl-icon {
background-image: svg-inline(ctrl-geolocate-white);
Expand Down
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 @@ -583,4 +583,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.classList.contains('maplibregl-ctrl-geolocate-hidden')).toBeTruthy();
});
});
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 @@ -532,6 +538,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.classList.add('maplibregl-ctrl-geolocate-hidden');
}

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