-
-
Notifications
You must be signed in to change notification settings - Fork 772
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
Fix GeolocateControl accuracy circle using turf.circle and GeoJSON #5440
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ | |
import type {IControl} from './control'; | ||
import {LngLatBounds} from '../../geo/lng_lat_bounds'; | ||
|
||
import * as turf from '@turf/circle'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use a "specific import" instead of "*"? |
||
import {type GeoJSONSource} from '../../source/geojson_source'; | ||
|
||
/** | ||
* The {@link GeolocateControl} options object | ||
*/ | ||
|
@@ -242,7 +245,6 @@ | |
options: GeolocateControlOptions; | ||
_container: HTMLElement; | ||
_dotElement: HTMLElement; | ||
_circleElement: HTMLElement; | ||
_geolocateButton: HTMLButtonElement; | ||
_geolocationWatchID: number; | ||
_timeoutId: ReturnType<typeof setTimeout>; | ||
|
@@ -265,7 +267,7 @@ | |
_watchState: 'OFF' | 'ACTIVE_LOCK' | 'WAITING_ACTIVE' | 'ACTIVE_ERROR' | 'BACKGROUND' | 'BACKGROUND_ERROR'; | ||
_lastKnownPosition: any; | ||
_userLocationDotMarker: Marker; | ||
_accuracyCircleMarker: Marker; | ||
_accuracyCirclePolygon: any; | ||
_accuracy: number; | ||
_setup: boolean; // set to true once the control has been setup | ||
|
||
|
@@ -298,12 +300,15 @@ | |
if (this.options.showUserLocation && this._userLocationDotMarker) { | ||
this._userLocationDotMarker.remove(); | ||
} | ||
if (this.options.showAccuracyCircle && this._accuracyCircleMarker) { | ||
this._accuracyCircleMarker.remove(); | ||
|
||
if (this.options.showAccuracyCircle && this._accuracyCirclePolygon) { | ||
if (this._map.loaded() && this._map.isStyleLoaded()) { | ||
this._map.removeLayer('accuracy-circle'); | ||
this._map.removeSource('accuracy-circle'); | ||
} | ||
} | ||
|
||
DOM.remove(this._container); | ||
this._map.off('zoom', this._onZoom); | ||
this._map = undefined; | ||
numberOfWatches = 0; | ||
noTimeout = false; | ||
|
@@ -447,32 +452,46 @@ | |
_updateMarker = (position?: GeolocationPosition | null) => { | ||
if (position) { | ||
const center = new LngLat(position.coords.longitude, position.coords.latitude); | ||
this._accuracyCircleMarker.setLngLat(center).addTo(this._map); | ||
this._userLocationDotMarker.setLngLat(center).addTo(this._map); | ||
this._accuracy = position.coords.accuracy; | ||
if (this.options.showUserLocation && this.options.showAccuracyCircle) { | ||
this._updateCircleRadius(); | ||
|
||
this._accuracyCirclePolygon = turf.circle([position.coords.longitude, position.coords.latitude], position.coords.accuracy, {steps: 64, | ||
units: 'meters'}); | ||
|
||
if (this._map.getSource('accuracy-circle')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will happen if a style has a source with this name that is not relevant to this circle? |
||
const geoJSONSource = this._map.getSource('accuracy-circle') as GeoJSONSource; | ||
geoJSONSource.setData({ | ||
'type': 'FeatureCollection', | ||
'features': [this._accuracyCirclePolygon] | ||
}); | ||
} else { | ||
if (this._map.loaded() && this._map.isStyleLoaded()) { | ||
this._map.addSource('accuracy-circle', { | ||
type: 'geojson', | ||
data: { | ||
'type': 'FeatureCollection', | ||
'features': [this._accuracyCirclePolygon] | ||
} | ||
}); | ||
this._map.addLayer({ | ||
id: 'accuracy-circle', | ||
type: 'fill', | ||
source: 'accuracy-circle', | ||
paint: { | ||
'fill-color': '#8CCFFF', | ||
'fill-opacity': 0.5 | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} else { | ||
this._userLocationDotMarker.remove(); | ||
this._accuracyCircleMarker.remove(); | ||
} | ||
}; | ||
|
||
_updateCircleRadius() { | ||
const bounds = this._map.getBounds(); | ||
const southEastPoint = bounds.getSouthEast(); | ||
const northEastPoint = bounds.getNorthEast(); | ||
const mapHeightInMeters = southEastPoint.distanceTo(northEastPoint); | ||
const mapHeightInPixels = this._map._container.clientHeight; | ||
const circleDiameter = Math.ceil(2 * (this._accuracy / (mapHeightInMeters / mapHeightInPixels))); | ||
this._circleElement.style.width = `${circleDiameter}px`; | ||
this._circleElement.style.height = `${circleDiameter}px`; | ||
} | ||
|
||
_onZoom = () => { | ||
if (this.options.showUserLocation && this.options.showAccuracyCircle) { | ||
this._updateCircleRadius(); | ||
if (this._map.loaded() && this._map.isStyleLoaded()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the style is still being loaded this might "leak"... |
||
this._map.removeLayer('accuracy-circle'); | ||
this._map.removeSource('accuracy-circle'); | ||
} | ||
} | ||
}; | ||
|
||
|
@@ -568,12 +587,7 @@ | |
|
||
this._userLocationDotMarker = new Marker({element: this._dotElement}); | ||
|
||
this._circleElement = DOM.create('div', 'maplibregl-user-location-accuracy-circle'); | ||
this._accuracyCircleMarker = new Marker({element: this._circleElement, pitchAlignment: 'map'}); | ||
|
||
if (this.options.trackUserLocation) this._watchState = 'OFF'; | ||
|
||
this._map.on('zoom', this._onZoom); | ||
} | ||
|
||
this._geolocateButton.addEventListener('click', () => this.trigger()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't keep commented out tests. Either bring them back or completely remove them.