├── readme.md └── index.js /readme.md: -------------------------------------------------------------------------------- 1 | # Distance IP 2 | > A function that results in a distance between two IP addresses 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const client = require('@bigdatacloudapi/client')('token'); 2 | 3 | function distance(lat1,lon1,lat2,lon2) { 4 | let R = 6371; 5 | let dLat = deg2rad(lat2-lat1); 6 | let dLon = deg2rad(lon2-lon1); 7 | let a = 8 | Math.sin(dLat/2) * Math.sin(dLat/2) + 9 | Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 10 | Math.sin(dLon/2) * Math.sin(dLon/2) 11 | ; 12 | let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 13 | let d = R * c; 14 | return d; 15 | } 16 | 17 | function deg2rad(deg) { 18 | return deg * (Math.PI/180) 19 | } 20 | 21 | (async () => { 22 | try { 23 | const response = await fetch('http://ip-api.com/json/tyt toje') 24 | const response_1 = await fetch('http://ip-api.com/json/tyt ip adress') 25 | const json = await response.json() 26 | const json_1 = await response_1.json() 27 | let result_dist = distance(json.lat, json.lon, json_1.lat, json_1.lon) 28 | if (result_dist != null || result_dist != undefined) { 29 | console.log('Result: \n' + ' [' + json.country + '/' + json.city + '] \n' + ' [' + json_1.country + '/' + json_1.city + ']\nDistance: ' + Math.round(result_dist, 1) + 'km') 30 | } 31 | console.log(json); 32 | } catch (error) { 33 | console.log(error); 34 | } 35 | })(); 36 | --------------------------------------------------------------------------------