mapbox / mapbox/mapbox-gl-js

Expose a "statechange" event on GeolocateControl

Open
#5,136 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature :green_apple: good first issue
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

## Motivation

Discussion started here: https://github.com/mapbox/mapbox-gl-js/pull/4479#issuecomment-321203924

The `GeolocateControl` doesn't expose events necessary to act on all of its states. `trackuserlocationstart` and `trackuserlocationend` aren't enough to detect some user actions. For instance, it's impossible to know if the user toggled off tracking.

To achieve this I currently have to use a `MutationObserver` on the `.mapboxgl-ctrl-geolocate` watching for `classList` changes.

## Design

Fire a new `statechange` event on every `_watchState` change and pass along its value.

### Implementation

Replace all `this._watchState = ...` with:
```javascript
_setState(state) => {
if (this._watchState !== state) {
this._watchState = state
this.fire('statechange', state)

// ... conditions logic to fire trackuserlocationstart and trackuserlocationend

// bonus?
this._updateUI(state) // centralize the different classList updates spread in _onSucces, _onError, _onClick...
}
}
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the GeolocateControl entry point and inspect each _watchState assignment and the existing control tests. Done means a statechange event is emitted for every state transition and carries the new state, without losing the existing tracking events.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.