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

Add Get Current Address App #3761

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions apps/getaddr/ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.01: Initial release
24 changes: 24 additions & 0 deletions apps/getaddr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Get Address App
This app uses the GPS and compass capabilities of the Bangle.js 2 watch to display your current location and direction.

The app uses the Nominatim API to reverse geocode your location and display the street and house number. It also uses the compass to display your current direction.

## Screenshots
![](image1.png)
![](image2.png)

## Requirements
To run this app, you will need to meet the following requirements:

1. Android Integration app or iOS integration app must be installed.
2. In Bangle.js Gadgetbridge connect the watch, then navigate to the **Gear icon** and enable "Use GPS data from phone".
3. In the same settings dialog scroll down and enable "Allow internet access".
4. Under **System** > **Settings** > **General Settings**, press "Get location" once and wait until it finds a location
5. In the same settings dialog enable "Keep location up to date".
7. The watch must have a clear view of the sky to receive GPS signals.
8. The compass must be calibrated by moving the watch in a figure-eight motion.

Note: This app requires an active internet connection to function. It uses the Nominatim API to reverse geocode your location, and it may not work in areas with limited or no internet connectivity.


by [Online Speech to Text Cloud](https://www.speech-to-text.cloud/)
1 change: 1 addition & 0 deletions apps/getaddr/getaddr-icon.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

157 changes: 157 additions & 0 deletions apps/getaddr/getaddr.app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Set the API endpoint and parameters
const nominatimApi = 'https://nominatim.openstreetmap.org';
const locale = require('locale');
let lang = locale.name;

if (lang.toLowerCase() === 'system') {
lang = 'en';
} else {
lang = lang.substring(0, 2);
}

const params = {
format: 'json',
addressdetails: 1,
zoom: 18,
extratags: 1
};

// Function to break a string into lines
function breakStringIntoLines(str, maxWidth) {
const words = str.split(' ');
const lines = [];
let currentLine = '';
for (const word of words) {
if (currentLine.length + word.length + 1 > maxWidth) {
lines.push(currentLine);
currentLine = word;
} else {
if (currentLine!== '') {
currentLine +=' ';
}
currentLine += word;
}
}
lines.push(currentLine);
return lines;
}

// Function to clear the screen and display a message
function showMessage(address, error, dir) {
g.clear();
g.reset();
g.setFontVector(16);
const addressLines = breakStringIntoLines(address, 20);
let y = 20;
for (const line of addressLines) {
g.drawString(line, 10, y);
y += 20;
}
if (error) {
y += 10;
const errorLines = breakStringIntoLines(error, 20);
for (const line of errorLines) {
g.drawString(line, 10, y);
y += 20;
}
}
g.drawString(`Direction: ${dir}`, 10, 150);
g.flip();
}

// Function to get the compass direction
function getCompassDirection() {
const compass = Bangle.getCompass();
if (compass && compass.heading) {
const direction = Math.floor(compass.heading);
const directions = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
const index = Math.floor(((direction % 360) + 22.5) / 45) % 8;
if (index >= 0 && index < directions.length) {
return directions[index];
} else {
return 'Invalid index';
}
} else {
return 'No compass data';
}
}

// Variable to store the current address and error
let currentAddress = 'Getting address...';
let currentError = '';
let lastUpdateTime = 0;

// Function to get the current location
function getCurrentLocation() {
Bangle.setGPSPower(1);
Bangle.setCompassPower(1);
Bangle.on('GPS', (gps) => {
if (gps.fix) {
const now = Date.now();
if (now - lastUpdateTime < 30000) return;
lastUpdateTime = now;
getStreetAndHouseNumber(gps.lat, gps.lon);
} else {
currentAddress = 'No GPS signal';
currentError = `Sats: ${gps.satellites}`;
showMessage(currentAddress, currentError, getCompassDirection());
}
});
}

// Function to get the street and house number
function getStreetAndHouseNumber(lat, lon) {
const url = `${nominatimApi}/reverse`;
const paramsStr = Object.keys(params).map(key => `${key}=${encodeURIComponent(params[key])}`).join('&');
const fullUrl = `${url}?${paramsStr}&lat=${lat}&lon=${lon}&accept-language=${lang}&format=json`;

Bangle.http(fullUrl).then(data => {
try {
const jsonData = JSON.parse(data.resp);
if (jsonData && jsonData.address) {
let street = jsonData.address.road;
if (street.includes('Straße')) {
street = street.replace('Straße', 'Str.');
} else if (street.includes('Street')) {
street = street.replace('Street', 'St.');
}
const houseNumber = jsonData.address.house_number;
const newAddress = `${street} ${houseNumber}`;
if (newAddress!== currentAddress) {
currentAddress = newAddress;
currentError = '';
}
} else {
const newAddress = 'No address';
if (newAddress!== currentAddress) {
currentAddress = newAddress;
currentError = '';
}
}
} catch (err) {
const newError = `Error: ${err}`;
if (newError!== currentError) {
currentError = newError;
}
}
showMessage(currentAddress, currentError, getCompassDirection());
}).catch(err => {
const newError = `Error: ${err}`;
if (newError!== currentError) {
currentError = newError;
}
showMessage(currentAddress, currentError, getCompassDirection());
});
}

// Main function
function main() {
showMessage('Getting address...', '', getCompassDirection());
getCurrentLocation();
setInterval(() => {
showMessage(currentAddress, currentError, getCompassDirection());
}, 1000);
}

// Call the main function
main();
Binary file added apps/getaddr/getaddr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/getaddr/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/getaddr/image2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions apps/getaddr/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "getaddr",
"name": "Get Addr",
"version": "0.01",
"description": "An app that shows the address of the current location",
"readme": "README.md",
"icon": "getaddr.png",
"screenshots": [{"url":"image1.png"}, {"url":"image2.png"}],
"type": "app",
"tags": "gps,outdoors,tools",
"supports": ["BANGLEJS2"],
"dependencies" : {},
"storage": [
{"name":"getaddr.app.js","url":"getaddr.app.js"},
{"name":"getaddr.img","url":"getaddr-icon.js","evaluate":true}
]
}