Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Created September 29, 2021 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netsi1964/1842cd23ed3c46811d3a1b3174a441cc to your computer and use it in GitHub Desktop.
Save netsi1964/1842cd23ed3c46811d3a1b3174a441cc to your computer and use it in GitHub Desktop.
A script for scriptable which will give a notification about if you are at a location (work) or not
const workLocation = { longitude: 10.21377573, latitude: 56.15343598 }; // DOKK1
const currentLocation = await Location.current();
const distanceToWork = calcCrow(
currentLocation.latitude,
currentLocation.longitude,
workLocation.latitude,
workLocation.longitude
).toFixed(1);
const notification = new Notification();
notification.title = 'Where are you?';
if (distanceToWork > 0.1) {
notification.sound = "complete";
notification.subtitle = "You are not at work 😀";
notification.body = '\nYou can relax';
notification.body = '\nYou should call the wife and inform that you will be late.';
} else {
notification.sound = "alert";
notification.subtitle = "You are at work";
}
notification.schedule();
//This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km)
function calcCrow(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = toRad(lat2 - lat1);
var dLon = toRad(lon2 - lon1);
var lat1 = toRad(lat1);
var lat2 = toRad(lat2);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
// Converts numeric degrees to radians
function toRad(Value) {
return (Value * Math.PI) / 180;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment