From bed450f08bbe78f4645167079417b0330870c80a Mon Sep 17 00:00:00 2001 From: Quincy Morgan Date: Thu, 27 Feb 2020 15:10:38 -0800 Subject: [PATCH] Don't enter modeBrowse if geolocation was unsuccessful Only set geolocation timeout when fetching the location Always fetch a new location when geolocating instead of only using the first one --- modules/ui/geolocate.js | 53 +++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/modules/ui/geolocate.js b/modules/ui/geolocate.js index 53f063550..6a54dc90d 100644 --- a/modules/ui/geolocate.js +++ b/modules/ui/geolocate.js @@ -10,9 +10,14 @@ import { uiLoading } from './loading'; import { uiTooltipHtml } from './tooltipHtml'; export function uiGeolocate(context) { - var geoOptions = { enableHighAccuracy: false, timeout: 6000 /* 6sec */ }; - var locating = uiLoading(context).message(t('geolocate.locating')).blocking(true); - var layer = context.layers().layer('geolocate'); + var _geolocationOptions = { + // prioritize speed and power usage over precision + enableHighAccuracy: false, + // don't hang indefinitely getting the location + timeout: 6000 // 6sec + }; + var _locating = uiLoading(context).message(t('geolocate.locating')).blocking(true); + var _layer = context.layers().layer('geolocate'); var _position; var _extent; var _timeoutID; @@ -20,26 +25,26 @@ export function uiGeolocate(context) { function click() { if (context.inIntro()) return; - context.enter(modeBrowse(context)); - if (!layer.enabled()) { - if (!_position) { - context.container().call(locating); - navigator.geolocation.getCurrentPosition(success, error, geoOptions); - } else { - zoomTo(); - } + if (!_layer.enabled()) { + + // This timeout ensures that we still call finish() even if + // the user declines to share their location in Firefox + _timeoutID = setTimeout(error, 10000 /* 10sec */ ); + + context.container().call(_locating); + // get the latest position even if we already have one + navigator.geolocation.getCurrentPosition(success, error, _geolocationOptions); } else { - layer.enabled(null, false); + _layer.enabled(null, false); updateButtonState(); } - // This timeout ensures that we still call finish() even if - // the user declines to share their location in Firefox - _timeoutID = setTimeout(error, 10000 /* 10sec */ ); } function zoomTo() { + context.enter(modeBrowse(context)); + var map = context.map(); - layer.enabled(_position, true); + _layer.enabled(_position, true); updateButtonState(); map.centerZoomEase(_extent.center(), Math.min(20, map.extentZoom(_extent))); } @@ -53,20 +58,26 @@ export function uiGeolocate(context) { } function error() { - uiFlash() - .text(t('geolocate.location_unavailable')) - .iconName('#iD-icon-geolocate')(); + if (_position) { + // use the position from a previous call if we have one + zoomTo(); + } else { + uiFlash() + .text(t('geolocate.location_unavailable')) + .iconName('#iD-icon-geolocate')(); + } + finish(); } function finish() { - locating.close(); // unblock ui + _locating.close(); // unblock ui if (_timeoutID) { clearTimeout(_timeoutID); } _timeoutID = undefined; } function updateButtonState() { - _button.classed('active', layer.enabled()); + _button.classed('active', _layer.enabled()); } return function(selection) {