aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/geolocation.js
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2017-11-15 17:35:29 +0000
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-11-15 17:35:29 +0000
commitdb75de1ca7268692215dc79f157d61ab7d66fa8e (patch)
treecb2095aa1f8466919b25674c35f7b8d1bbbabcde /web/js/geolocation.js
parent52ab72ad43b391b0f3b6f7385b5ba723cc1a4aba (diff)
parentf59e12ee3fa9c65f178a300954586f6ac91bba79 (diff)
Merge branch '1901-js-deferring'
Updated geolocation.js with concurrent changes.
Diffstat (limited to 'web/js/geolocation.js')
-rw-r--r--web/js/geolocation.js42
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';
+ }
+})();