Skip to content

Commit

Permalink
add appropriate listener reference for window
Browse files Browse the repository at this point in the history
  • Loading branch information
Takuto Suzuki committed Jul 5, 2021
1 parent f3f0b15 commit ea099f8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ui/control/geolocate_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class GeolocateControl extends Evented {
_setup: boolean; // set to true once the control has been setup
_heading: ?number;
_updateMarkerRotationThrottled: Function;
_onDeviceOrientationListener: Function;

constructor(options: Options) {
super();
Expand All @@ -140,6 +141,8 @@ class GeolocateControl extends Evented {
'_updateMarkerRotation'
], this);

// by referencing the function with .bind(), we can correctly remove from window's event listeners
this._onDeviceOrientationListener = this._onDeviceOrientation.bind(this)
this._updateMarkerRotationThrottled = throttle(this._updateMarkerRotation, 20);
}

Expand Down Expand Up @@ -595,7 +598,7 @@ class GeolocateControl extends Evented {
this._onSuccess, this._onError, positionOptions);

if (this.options.showUserHeading) {
window.addEventListener('deviceorientation', this._onDeviceOrientation.bind(this));
window.addEventListener('deviceorientation', this._onDeviceOrientationListener);
}
}
} else {
Expand All @@ -613,7 +616,7 @@ class GeolocateControl extends Evented {
_clearWatch() {
window.navigator.geolocation.clearWatch(this._geolocationWatchID);

window.removeEventListener('deviceorientation', this._onDeviceOrientation);
window.removeEventListener('deviceorientation', this._onDeviceOrientationListener);

this._geolocationWatchID = (undefined: any);
this._geolocateButton.classList.remove('mapboxgl-ctrl-geolocate-waiting');
Expand Down

0 comments on commit ea099f8

Please sign in to comment.