diff options
author | Marius Halden <marius.h@lden.org> | 2017-12-20 01:06:27 +0100 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2017-12-20 01:06:27 +0100 |
commit | 8b10c8b8d787e680bb085e2a7543dac50fd39742 (patch) | |
tree | 391efde6de4944e350cc0dafbd4e1efb0613e9c3 /web/js/geolocation.js | |
parent | eef35397ccf8242cdc65dc666db2958b1ee35440 (diff) | |
parent | 7e15bd3db202363db4bab8fa7c9f462eabe28fce (diff) |
Merge tag 'v2.3' into fiksgatami-dev
Diffstat (limited to 'web/js/geolocation.js')
-rw-r--r-- | web/js/geolocation.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/web/js/geolocation.js b/web/js/geolocation.js new file mode 100644 index 000000000..3c2cf04df --- /dev/null +++ b/web/js/geolocation.js @@ -0,0 +1,43 @@ +var fixmystreet = fixmystreet || {}; + +fixmystreet.geolocate = function(element, success_callback) { + element.addEventListener('click', function(e) { + var link = this; + e.preventDefault(); + link.className += ' loading'; + navigator.geolocation.getCurrentPosition(function(pos) { + link.className = link.className.replace(/loading/, ' '); + success_callback(pos); + }, function(err) { + link.className = link.className.replace(/loading/, ' '); + if (err.code === 1) { // User said no + link.innerHTML = translation_strings.geolocation_declined; + } else if (err.code === 2) { // No position + link.innerHTML = translation_strings.geolocation_no_position; + } else if (err.code === 3) { // Too long + link.innerHTML = translation_strings.geolocation_no_result; + } else { // Unknown + link.innerHTML = translation_strings.geolocation_unknown; + } + }, { + enableHighAccuracy: true, + timeout: 10000 + }); + }); +}; + +(function(){ + var link = document.getElementById('geolocate_link'); + if (!link) { return; } + var https = window.location.protocol.toLowerCase() === 'https:'; + if ('geolocation' in navigator && https) { + fixmystreet.geolocate(link, function(pos) { + var latitude = pos.coords.latitude.toFixed(6); + var longitude = pos.coords.longitude.toFixed(6); + var coords = 'latitude=' + latitude + ';longitude=' + longitude; + location.href = link.href + (link.href.indexOf('?') > -1 ? ';' : '?') + coords; + }); + } else { + link.style.display = 'none'; + } +})(); |