diff options
Diffstat (limited to 'web/js/geolocation.js')
-rw-r--r-- | web/js/geolocation.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/web/js/geolocation.js b/web/js/geolocation.js new file mode 100644 index 000000000..5ff9fe9dc --- /dev/null +++ b/web/js/geolocation.js @@ -0,0 +1,42 @@ +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; } + if ('geolocation' in navigator) { + 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'; + } +})(); |