Created
May 9, 2018 13:48
-
-
Save Danny-Engelman/d418a59a70da1851d4e4d5e9bdf25364 to your computer and use it in GitHub Desktop.
TTN Gateways near my lat/lon location (and distance)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
nearbyGateways(52.4, 4.87); // latitude, longitude | |
function nearbyGateways(lat, lon, meters = 15000) { | |
fetch(`https://www.thethingsnetwork.org/gateway-data/location?latitude=${lat}&longitude=${lon}&distance=${meters}`) | |
.then(response => response.json()) | |
.then(json => { | |
let $_Distance = (lat1, lon1, lat2, lon2, accuracy = 1e3) => { // Haversine distance | |
let M = Math, C = M.cos, P = M.PI / 180, | |
a = 0.5 - C((lat2 - lat1) * P) / 2 + C(lat1 * P) * C(lat2 * P) * (1 - C((lon2 - lon1) * P)) / 2; | |
return M.round(M.asin(M.sqrt(a)) * 12742 * accuracy) / accuracy;// 12742 = Earth radius KMs * 2 | |
}; | |
console.table(Object.keys(json).map(id => { | |
let gtw = json[id]; | |
gtw.distance = $_Distance(lat, lon, gtw.lat = gtw.location.latitude, gtw.lon = gtw.location.longitude); | |
return gtw; | |
}), ["id", "owner", "description", "last_seen", "lat", "lon", "distance"]); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment