From b78f6513de3aaec2686685a979005d725e8d62b0 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Mon, 7 Aug 2017 11:15:51 -0400 Subject: [PATCH] Use native altutude data --- index.html | 4 +--- script.js | 62 ++++++++++++------------------------------------------ 2 files changed, 15 insertions(+), 51 deletions(-) diff --git a/index.html b/index.html index 5e42897..c65c88b 100644 --- a/index.html +++ b/index.html @@ -50,8 +50,7 @@
0.00000 N
0.00000 E
-
0.0 m
-
No altitude data available.
+
@@ -71,7 +70,6 @@ - diff --git a/script.js b/script.js index d8f74b9..50ecb4e 100644 --- a/script.js +++ b/script.js @@ -1,5 +1,5 @@ 'use strict'; -/* global navigator google $ */ +/* global navigator $ */ // Set units based on browser locale const metric = !(window.navigator.language=='en-US'||window.navigator.language=='my'); @@ -10,13 +10,19 @@ else { navigator.geolocation.watchPosition( // Got location function(pos) { - setAltitude(pos.coords.latitude, pos.coords.longitude); - var lat = pos.coords.latitude.toFixed(4); - var lon = pos.coords.longitude.toFixed(4); - lat = (lat.substring(0,1)=='-')? lat.substring(1)+' S' : lat+' N'; - lon = (lon.substring(0,1)=='-')? lon.substring(1)+' E' : lon+' W'; - $('#lat').text(lat); - $('#lon').text(lon); + let lat = pos.coords.latitude.toFixed(4); + let lon = pos.coords.longitude.toFixed(4); + $('#lat').text((lat.substring(0,1)=='-')? lat.substring(1)+' S' : lat+' N'); + $('#lon').text((lon.substring(0,1)=='-')? lon.substring(1)+' E' : lon+' W'); + + // Get altitude + if (pos.coords.altitude){ + var alt = (metric)? pos.coords.altitude.toFixed(1)+' m':(pos.coords.altitude*0.3048).toFixed(1)+' ft'; + $('#alt').show().text(alt); + } + else { + $('#alt').hide(); + } }, // Got error @@ -32,46 +38,6 @@ else { navigator.geolocation.watchPosition( ); } -// Set altitude -function setAltitude(lat,lon) { - - // Create elevator - if (typeof elevator == 'undefined') { - var elevator = new google.maps.ElevationService; - } - - // Query API - else { - elevator.getElevationForLocations({ - 'locations': [ new google.maps.LatLng(lat,lon) ] - }, function(res) { - - // Show error - if (!res[0].elevation) { - if (res.status && res.status!='OK') { - $('#no-alt').text('No altitude data available: '+res.status+'. '); - } - $('#no-alt').show(); - } - - // Successfully got altitude - else { - $('#no-alt').hide(); - var alt = res[0].elevation; - - // Convert - if (metric) { alt=alt.toFixed(1)+' m'; } - else { alt=(alt*0.3048).toFixed(1)+' ft'; } - - // Set element text - $('#alt').text(alt); - - } - - }); - } -} - // Set compass orientation function setRose(e) {