Guest User

Untitled

a guest
Oct 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. const filterItemsByRadius = (userRadius, items) => {
  2. axios
  3. .get('http://ip-api.com/json')
  4. .then(response => {
  5. const data = [];
  6.  
  7. const { lat, lon } = response.data;
  8. items.map(item => {
  9. let itemGeolocation;
  10. if (item.geolocation) {
  11. itemGeolocation = item.geolocation.coords;
  12. }
  13.  
  14. const currentLocation = {
  15. latitude: lat,
  16. longitude: lon,
  17. };
  18. const distanceArr = geolib.orderByDistance(currentLocation, [itemGeolocation]);
  19. const miles = (distanceArr[0].distance / 1609.34).toFixed(2);
  20. if (miles <= userRadius) {
  21. data.push(item);
  22. return data;
  23. }
  24. });
  25. })
  26. .catch(err => {
  27. console.log(err);
  28. });
  29. };
  30.  
  31. console.log(filterItemsByRadius(userRadius, items));
Add Comment
Please, Sign In to add comment