Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openstmap: Remember latitude, longitude & scale #2864

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/openstmap/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
0.17: With new Recorder app allow track to be drawn in the background
Switch tile layer URL for faster/more reliable map tiles
0.18: Prefer map with highest resolution
0.19: Remember latitude, longitude & scale
50 changes: 39 additions & 11 deletions apps/openstmap/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ var mapVisible = false;
var hasScrolled = false;
var settings = require("Storage").readJSON("openstmap.json",1)||{};
var plotTrack;
let checkMapPos = false; // Do we need to check the if the coordinates we have are valid

if (settings.lat !== undefined && settings.lon !== undefined && settings.scale !== undefined) {
// restore last view
m.lat = settings.lat;
m.lon = settings.lon;
m.scale = settings.scale;
checkMapPos = true;
}

// Redraw the whole page
function redraw() {
g.setClipRect(R.x,R.y,R.x2,R.y2);
m.draw();
const count = m.draw();
if (checkMapPos && count === 0) {
// no map at these coordinates, lets try again with first map
m.lat = m.map.lat;
m.lon = m.map.lon;
m.scale = m.map.scale;
m.draw();
}
drawPOI();
drawMarker();
// if track drawing is enabled...
Expand Down Expand Up @@ -45,7 +61,7 @@ function drawPOI() {
g.fillRect(p.x-sz, p.y-sz, p.x+sz, p.y+sz);
g.setColor(0,0,0);
g.drawString(wp.name, p.x, p.y);
print(wp.name);
//print(wp.name);
})
}

Expand All @@ -59,24 +75,33 @@ function drawMarker() {

Bangle.on('GPS',function(f) {
fix=f;
if (HASWIDGETS) WIDGETS["sats"].draw(WIDGETS["sats"]);
if (HASWIDGETS && WIDGETS["sats"]) WIDGETS["sats"].draw(WIDGETS["sats"]);
if (mapVisible) drawMarker();
});
Bangle.setGPSPower(1, "app");

if (HASWIDGETS) {
Bangle.loadWidgets();
WIDGETS["sats"] = { area:"tl", width:48, draw:w=>{
var txt = (0|fix.satellites)+" Sats";
if (!fix.fix) txt += "\nNO FIX";
g.reset().setFont("6x8").setFontAlign(0,0)
.drawString(txt,w.x+24,w.y+12);
}
};
if (!WIDGETS["gps"]) { // one GPS Widget is enough
WIDGETS["sats"] = { area:"tl", width:48, draw:w=>{
var txt = (0|fix.satellites)+" Sats";
if (!fix.fix) txt += "\nNO FIX";
g.reset().setFont("6x8").setFontAlign(0,0)
.drawString(txt,w.x+24,w.y+12);
}
};
}
Bangle.drawWidgets();
}
R = Bangle.appRect;

function writeSettings() {
settings.lat = m.lat;
settings.lon = m.lon;
settings.scale = m.scale;
require("Storage").writeJSON("openstmap.json",settings);
}

function showMap() {
mapVisible = true;
g.reset().clearRect(R);
Expand Down Expand Up @@ -108,7 +133,7 @@ function showMap() {
},
/*LANG*/"Draw Track": {
value : !!settings.drawTrack,
onchange : v => { settings.drawTrack=v; require("Storage").writeJSON("openstmap.json",settings); }
onchange : v => { settings.drawTrack=v; writeSettings(); }
},
/*LANG*/"Center Map": () =>{
m.lat = m.map.lat;
Expand All @@ -126,3 +151,6 @@ function showMap() {
}

showMap();

// Write settings on exit via button
setWatch(() => writeSettings(), BTN, { repeat: true, edge: "rising" });
2 changes: 1 addition & 1 deletion apps/openstmap/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "openstmap",
"name": "OpenStreetMap",
"shortName": "OpenStMap",
"version": "0.18",
"version": "0.19",
"description": "Loads map tiles from OpenStreetMap onto your Bangle.js and displays a map of where you are. Once installed this also adds map functionality to `GPS Recorder` and `Recorder` apps",
"readme": "README.md",
"icon": "app.png",
Expand Down
7 changes: 6 additions & 1 deletion apps/openstmap/openstmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ m.scale = m.map.scale; // current scale (based on first map)
m.lat = m.map.lat; // position of middle of screen
m.lon = m.map.lon; // position of middle of screen

// return number of tiles drawn
exports.draw = function() {
var cx = g.getWidth()/2;
var cy = g.getHeight()/2;
var p = Bangle.project({lat:m.lat,lon:m.lon});
let count = 0;
m.maps.forEach((map,idx) => {
var d = map.scale/m.scale;
var ix = (p.x-map.center.x)/m.scale + (map.imgx*d/2) - cx;
Expand Down Expand Up @@ -68,12 +70,15 @@ exports.draw = function() {
}
var mx = g.getWidth();
var my = g.getHeight();
for (var x=ox,ttx=tx; x<mx && ttx<map.w; x+=s,ttx++)
for (var x=ox,ttx=tx; x<mx && ttx<map.w; x+=s,ttx++) {
for (var y=oy,tty=ty;y<my && tty<map.h;y+=s,tty++) {
o.frame = ttx+(tty*map.w);
g.drawImage(img,x,y,o);
count++;
}
}
});
return count;
};

/// Convert lat/lon to pixels on the screen
Expand Down
Loading