Merge pull request #5629 from openstreetmap/5587

5587 - make geolocation show geolocation
This commit is contained in:
Bryan Housel
2018-12-19 12:14:36 -05:00
committed by GitHub
5 changed files with 168 additions and 11 deletions
+25 -9
View File
@@ -9,27 +9,43 @@ import { uiLoading } from './loading';
export function uiGeolocate(context) {
var geoOptions = { enableHighAccuracy: false, timeout: 6000 /* 6sec */ },
locating = uiLoading(context).message(t('geolocate.locating')).blocking(true),
layer = context.layers().layer('geolocate'),
position,
extent,
timeoutId;
function click() {
if (context.inIntro()) return;
context.enter(modeBrowse(context));
context.container().call(locating);
navigator.geolocation.getCurrentPosition(success, error, geoOptions);
if (!layer.enabled()) {
if (!position) {
context.container().call(locating);
navigator.geolocation.getCurrentPosition(success, error, geoOptions);
} else {
zoomTo();
}
} else {
layer.enabled(null, false);
}
// This timeout ensures that we still call finish() even if
// the user declines to share their location in Firefox
timeoutId = setTimeout(finish, 10000 /* 10sec */ );
}
function success(position) {
var map = context.map(),
extent = geoExtent([position.coords.longitude, position.coords.latitude])
.padByMeters(position.coords.accuracy);
function zoomTo() {
var map = context.map();
layer.enabled(position, true);
map.centerZoom(extent.center(), Math.min(20, map.extentZoom(extent)));
}
function success(geolocation) {
position = geolocation;
extent = geoExtent([position.coords.longitude, position.coords.latitude])
.padByMeters(position.coords.accuracy);
zoomTo();
finish();
}