mapbox/mapbox-gl-js

Expose a "statechange" event on GeolocateControl

Open

#5.136 geöffnet am 11. Aug. 2017

Auf GitHub ansehen
 (0 Kommentare) (2 Reaktionen) (0 zugewiesene Personen)JavaScript (2.203 Forks)batch import
feature :green_apple:good first issue

Repository-Metriken

Stars
 (10.532 Stars)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

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:

_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