Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const filterItemsByRadius = (userRadius, items) => {
- axios
- .get('http://ip-api.com/json')
- .then(response => {
- const data = [];
- const { lat, lon } = response.data;
- items.map(item => {
- let itemGeolocation;
- if (item.geolocation) {
- itemGeolocation = item.geolocation.coords;
- }
- const currentLocation = {
- latitude: lat,
- longitude: lon,
- };
- const distanceArr = geolib.orderByDistance(currentLocation, [itemGeolocation]);
- const miles = (distanceArr[0].distance / 1609.34).toFixed(2);
- if (miles <= userRadius) {
- data.push(item);
- return data;
- }
- });
- })
- .catch(err => {
- console.log(err);
- });
- };
- console.log(filterItemsByRadius(userRadius, items));
Add Comment
Please, Sign In to add comment