diff --git a/config-route.js b/config-route.js index 72189d9..fdcc569 100644 --- a/config-route.js +++ b/config-route.js @@ -183,25 +183,31 @@ function rebuildSuggestions() { if (nearbyStatus === 'notRequested') { suggestionsArea.appendChild(a('', 'Load nearby stations').event$('click', function (event) { event.preventDefault() + var latitude = 0 + var longitude = 0 var watchId = navigator.geolocation.watchPosition( function (data) { - var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby') - geoUrl.searchParams.append('latitude', data.coords.latitude.toString()) - geoUrl.searchParams.append('longitude', data.coords.longitude.toString()) - geoUrl.searchParams.append('results', '10') - fetch(geoUrl) - .then(function (response) { - return response.json() - }) - .then(function (data) { - nearbyStatus = 'gotData' - nearbyStations = data - rebuildSuggestions() - }) - .catch(function () { - nearbyStatus = 'unavailable' - rebuildSuggestions() - }) + if (data.coords.latitude !== latitude || data.coords.longitude !== longitude) { + latitude = data.coords.latitude + longitude = data.coords.longitude + var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby') + geoUrl.searchParams.append('latitude', data.coords.latitude.toString()) + geoUrl.searchParams.append('longitude', data.coords.longitude.toString()) + geoUrl.searchParams.append('results', '10') + fetch(geoUrl) + .then(function (response) { + return response.json() + }) + .then(function (data) { + nearbyStatus = 'gotData' + nearbyStations = data + rebuildSuggestions() + }) + .catch(function () { + nearbyStatus = 'unavailable' + rebuildSuggestions() + }) + } }, function (error) { if (nearbyStations.length === 0) { diff --git a/station.js b/station.js index d552873..258f641 100755 --- a/station.js +++ b/station.js @@ -145,25 +145,31 @@ function rebuildSuggestions() { if (nearbyStatus === 'notRequested') { suggestionsArea.appendChild(a('', 'Load nearby stations').event$('click', function (event) { event.preventDefault() + var latitude = 0 + var longitude = 0 var watchId = navigator.geolocation.watchPosition( function (data) { - var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby') - geoUrl.searchParams.append('latitude', data.coords.latitude.toString()) - geoUrl.searchParams.append('longitude', data.coords.longitude.toString()) - geoUrl.searchParams.append('results', '10') - fetch(geoUrl) - .then(function (response) { - return response.json() - }) - .then(function (data) { - nearbyStatus = 'gotData' - nearbyStations = data - rebuildSuggestions() - }) - .catch(function () { - nearbyStatus = 'unavailable' - rebuildSuggestions() - }) + if (data.coords.latitude !== latitude || data.coords.longitude !== longitude) { + latitude = data.coords.latitude + longitude = data.coords.longitude + var geoUrl = new URL('https://v6.db.transport.rest/locations/nearby') + geoUrl.searchParams.append('latitude', data.coords.latitude.toString()) + geoUrl.searchParams.append('longitude', data.coords.longitude.toString()) + geoUrl.searchParams.append('results', '10') + fetch(geoUrl) + .then(function (response) { + return response.json() + }) + .then(function (data) { + nearbyStatus = 'gotData' + nearbyStations = data + rebuildSuggestions() + }) + .catch(function () { + nearbyStatus = 'unavailable' + rebuildSuggestions() + }) + } }, function (error) { if (nearbyStations.length === 0) {