MyLocation - adds menu to set preferred city or setup from GPS

pull/975/head
hughbarney 2021-11-26 20:25:28 +00:00
parent 94f8b4bca7
commit ce0e50ed94
10 changed files with 163 additions and 0 deletions

View File

@ -4456,5 +4456,20 @@
"storage": [
{"name":"timecal.app.js","url":"timecal.app.js"}
]
},
{ "id": "mylocation",
"name": "My Location",
"shortName":"My Location",
"icon": "mylocation.png",
"screenshots": [{"url":"screenshot_1.png"}],
"version":"0.01",
"description": "My Locations adds a menu in the Settings app to set your preferred City or it can be set from the GPS. mylocation.json can be used by other apps that need your main location lat and lon. See README",
"readme": "README.md",
"tags": "tool,utility",
"type": "app",
"supports": ["BANGLEJS", "BANGLEJS2"],
"storage": [
{"name":"mylocation.settings.js","url":"mylocation.settings.js"}
]
}
]

View File

@ -0,0 +1 @@
0.01: First release

44
apps/mylocation/README.md Normal file
View File

@ -0,0 +1,44 @@
# My Location
*a adds a My Location settings menu, stores GPS lat and lon of your preferred city*
* My Location is a service app that adds a 'My Location' menu to the settings menu
* Select one of the preset Cities or setup through the GPS
* Other Apps can read this information to do calculations based on location
* When the City shows ??? it means the location has been set through the GPS
## Example Code
const LOCATION_FILE = "mylocation.json";
let location;
// requires the myLocation app
function loadLocation() {
location = require("Storage").readJSON(LOCATION_FILE,1)||{"lat":51.5072,"lon":0.1276,"location":"London"};
}
## Screenshots
### Select one of the Preset Cities
* The presets are London, Newcastle, Edinbrough, Paris, New York, Tokyo
![](screenshot_1.png)
### Or select 'Set By GPS' to start the GPS
![](screenshot_2.png)
### While the GPS is running you will see:
![](screenshot_3.png)
### When a GPS fix is received you will see:
![](screenshot_4.png)
## Creator
[Hugh Barney](https://github.com/hughbarney) For support and discussion please post in the [Bangle JS Forum](http://forum.espruino.com/microcosms/1424/)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -0,0 +1,73 @@
(function(back) {
const SETTINGS_FILE = "mylocation.json";
// initialize with default settings...
let s = {
'lat': 51.5072,
'lon': 0.1276,
'location': "London"
}
// ...and overwrite them with any saved values
// This way saved values are preserved if a new version adds more settings
const storage = require('Storage')
let settings = storage.readJSON(SETTINGS_FILE, 1) || {}
const saved = settings || {}
for (const key in saved) {
s[key] = saved[key]
}
function save() {
settings = s
storage.write(SETTINGS_FILE, settings)
}
const locations = ["London", "Newcastle", "Edinburgh", "Paris", "New York", "Tokyo","???"];
const lats = [51.5072 ,54.9783 ,55.9533 ,48.8566 ,40.7128 ,35.6762, 0.0];
const lons = [-0.1276 ,-1.6178 ,-3.1883 ,2.3522 , -74.0060 ,139.6503, 0.0];
E.showMenu({
'': { 'title': 'My Location' },
'< Back': back,
'City': {
value: 0 | locations.indexOf(s.location),
min: 0, max: 6,
format: v => locations[v],
onchange: v => {
if (v != 6) {
s.location = locations[v];
s.lat = lats[v];
s.lon = lons[v];
save();
}
},
},
'Set From GPS': ()=>setFromGPS()
})
function setFromGPS() {
console.log("set from GPS");
Bangle.setGPSPower(1);
E.showMessage("Waiting for GPS fix. Place watch in the open. Could take 10 minutes. Long press to abort", "GPS Running");
Bangle.setUI("updown", undefined);
}
Bangle.on('GPS', (gps) => {
//console.log(".");
if (gps.fix === 0) return;
//console.log("fix from GPS");
s = {'lat': gps.lat, 'lon': gps.lon, 'location': '???' }
Bangle.buzz(1500); // buzz on first position
Bangle.setGPSPower(0);
save();
Bangle.setUI("updown", ()=>{ load() });
E.showPrompt("Location has been saved from the GPS fix",{
title:"Location Saved",
buttons : {"OK":1}
}).then(function(v) {
load(); // load default clock
});
});
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@ -1,6 +1,9 @@
var SunCalc = require("https://raw.githubusercontent.com/mourner/suncalc/master/suncalc.js");
require("f_latosmall").add(Graphics);
const SETTINGS_FILE = "pastel.json";
const LOCATION_FILE = "mylocation.json";
let settings;
let location;
function loadSettings() {
settings = require("Storage").readJSON(SETTINGS_FILE,1)||{};
@ -8,6 +11,28 @@ function loadSettings() {
settings.font = settings.font||"Lato";
}
// requires the myLocation app
function loadLocation() {
location = require("Storage").readJSON(LOCATION_FILE,1)||{"lat":51.5072,"lon":0.1276,"location":"London"};
}
function extractTime(d){
var h = d.getHours(), m = d.getMinutes();
return(("0"+h).substr(-2) + ":" + ("0"+m).substr(-2));
}
var sunRise = "00:00";
var sunSet = "00:00";
function updateSunRiseSunSet(now, lat, lon, line){
// get today's sunlight times for lat/lon
var times = SunCalc.getTimes(new Date(), lat, lon);
// format sunrise time from the Date object
sunRise = extractTime(times.sunrise);
sunSet = extractTime(times.sunset);
}
function loadFonts() {
// load font files based on settings.font
if (settings.font == "Architect")
@ -39,6 +64,8 @@ const infoData = {
ID_BLANK: { calc: () => '' },
ID_DATE: { calc: () => {var d = (new Date).toString().split(" "); return d[2] + ' ' + d[1] + ' ' + d[3];} },
ID_DAY: { calc: () => {var d = require("locale").dow(new Date).toLowerCase(); return d[0].toUpperCase() + d.substring(1);} },
ID_SR: { calc: () => 'Sunrise: ' + sunRise },
ID_SS: { calc: () => 'Sunset: ' + sunSet },
ID_STEP: { calc: () => 'Steps: ' + stepsWidget().getSteps() },
ID_BATT: { calc: () => 'Battery: ' + E.getBattery() + '%' },
ID_MEM: { calc: () => {var val = process.memory(); return 'Ram: ' + Math.round(val.usage*100/val.total) + '%';} },
@ -169,6 +196,9 @@ Bangle.setUI("clockupdown", btn=> {
loadSettings();
loadFonts();
loadLocation();
updateSunRiseSunSet(new Date(), location.lat, location.lon);
g.clear();
var secondInterval = setInterval(draw, 1000);
draw();