compass/coordinates.js

40 lines
972 B
JavaScript
Raw Normal View History

2018-08-30 12:01:11 -06:00
'use strict'
/* global navigator $ */
2017-03-29 06:02:16 -06:00
// Set units based on browser locale
const metric = !(window.navigator.language=='en-US'||window.navigator.language=='my');
2018-08-30 12:01:11 -06:00
// Track location
if (!navigator.geolocation){ $('#no-gps').show(); }
2017-03-29 06:02:16 -06:00
else { navigator.geolocation.watchPosition(
// Got location
function(pos) {
2017-08-07 09:15:51 -06:00
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();
}
2017-03-29 06:02:16 -06:00
},
// Got error
function() {
$('.coord').hide();
2017-03-29 01:45:56 -06:00
$('#rotated').hide();
$('#no-gps').show();
},
2017-03-29 06:02:16 -06:00
// Options
{ enableHighAccuracy:true }
2017-03-29 06:02:16 -06:00
);
}