aboutsummaryrefslogtreecommitdiffstats
path: root/web/js/geolocation.js
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2017-11-14 16:05:55 +0000
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-11-15 16:01:22 +0000
commit57fc2d335b97f2c692ce0575787be3164d8388f4 (patch)
tree15b66491e4cb2332989e4510509022669ef95705 /web/js/geolocation.js
parent4daf81dfcba97665d6a9beec86bb2bb3fe836be7 (diff)
Factor out geolocation to pages that need it.
Diffstat (limited to 'web/js/geolocation.js')
-rw-r--r--web/js/geolocation.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/web/js/geolocation.js b/web/js/geolocation.js
new file mode 100644
index 000000000..a5c229c19
--- /dev/null
+++ b/web/js/geolocation.js
@@ -0,0 +1,41 @@
+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;
+ var longitude = pos.coords.longitude;
+ location.href = link.href + '?latitude=' + latitude + ';longitude=' + longitude;
+ });
+ } else {
+ link.style.display = 'none';
+ }
+})();