Created
September 16, 2021 11:05
-
-
Save akimabs/059b8f7d528570926fc5b72a0d6d6d1c to your computer and use it in GitHub Desktop.
harversine with sort ala ala
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
const data = { | |
Fields: [ | |
{ | |
id: "3e88bbb7-f93c-4038-94ed-8870f8a5a454", | |
name: "GOR Jakarta Mantep", | |
address: "Jl, Sudirman Punya Gela", | |
latitude: -6.392687, | |
longitude: 106.389778, | |
createdAt: "2021-09-16T01:51:31.288Z", | |
updatedAt: "2021-09-16T01:51:31.288Z", | |
deletedAt: null, | |
}, | |
{ | |
id: "3e88bbb7-f93c-4038-94ed-8870f8a5a454", | |
name: "GOR Jakarta Juga", | |
address: "Jl, Sudirman Punya anjay", | |
latitude: -6.292687, | |
longitude: 106.889778, | |
createdAt: "2021-09-16T01:51:31.288Z", | |
updatedAt: "2021-09-16T01:51:31.288Z", | |
deletedAt: null, | |
}, | |
{ | |
id: "3e88bbb7-f93c-4038-94ed-8870f8a5a454", | |
name: "GOR Jakarta", | |
address: "Jl, Sudirman Punya", | |
latitude: -6.192687, | |
longitude: 106.789778, | |
createdAt: "2021-09-16T01:51:31.288Z", | |
updatedAt: "2021-09-16T01:51:31.288Z", | |
deletedAt: null, | |
}, | |
], | |
}; | |
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) { | |
var R = 6371; // Radius of the earth in km | |
var dLat = deg2rad(lat2 - lat1); // deg2rad below | |
var dLon = deg2rad(lon2 - lon1); | |
var a = | |
Math.sin(dLat / 2) * Math.sin(dLat / 2) + | |
Math.cos(deg2rad(lat1)) * | |
Math.cos(deg2rad(lat2)) * | |
Math.sin(dLon / 2) * | |
Math.sin(dLon / 2); | |
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
var d = R * c; // Distance in km | |
return d; | |
} | |
function deg2rad(deg) { | |
return deg * (Math.PI / 180); | |
} | |
function main() { | |
const { Fields } = data; | |
const myLocation = { latitude: -6.192687, longitude: 106.789778 }; | |
const tmpArray = []; | |
Fields.map((res) => { | |
const distance = getDistanceFromLatLonInKm( | |
myLocation.latitude, | |
myLocation.longitude, | |
res.latitude, | |
res.longitude | |
); | |
tmpArray.push({ | |
...res, | |
distance, | |
}); | |
}); | |
const dataWithSort = tmpArray.sort((a, b) => a.distance - b.distance); | |
console.log(dataWithSort); | |
} | |
main(); | |
// And will return | |
// | |
// [ | |
// { | |
// id: '3e88bbb7-f93c-4038-94ed-8870f8a5a454', | |
// name: 'GOR Jakarta', | |
// address: 'Jl, Sudirman Punya', | |
// latitude: -6.192687, | |
// longitude: 106.789778, | |
// createdAt: '2021-09-16T01:51:31.288Z', | |
// updatedAt: '2021-09-16T01:51:31.288Z', | |
// deletedAt: null, | |
// distance: 0 | |
// }, | |
// { | |
// id: '3e88bbb7-f93c-4038-94ed-8870f8a5a454', | |
// name: 'GOR Jakarta Juga', | |
// address: 'Jl, Sudirman Punya anjay', | |
// latitude: -6.292687, | |
// longitude: 106.889778, | |
// createdAt: '2021-09-16T01:51:31.288Z', | |
// updatedAt: '2021-09-16T01:51:31.288Z', | |
// deletedAt: null, | |
// distance: 15.678781766552461 | |
// }, | |
// { | |
// id: '3e88bbb7-f93c-4038-94ed-8870f8a5a454', | |
// name: 'GOR Jakarta Mantep', | |
// address: 'Jl, Sudirman Punya Gela', | |
// latitude: -6.392687, | |
// longitude: 106.389778, | |
// createdAt: '2021-09-16T01:51:31.288Z', | |
// updatedAt: '2021-09-16T01:51:31.288Z', | |
// deletedAt: null, | |
// distance: 49.48831606381643 | |
// } | |
// ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment