MapHelpers.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // @flow
  2. import R from 'ramda'
  3. export const removeEmpty = (markers: Array<Object>) => {
  4. let filteredMarkers = R.filter((item) => {
  5. return item.latitude && item.longitude
  6. }, markers)
  7. return filteredMarkers
  8. }
  9. export const calculateRegion = (locations: Array<Object>, options: Object) => {
  10. const latPadding = options && options.latPadding ? options.latPadding : 0.1
  11. const longPadding = options && options.longPadding ? options.longPadding : 0.1
  12. const mapLocations = removeEmpty(locations)
  13. // Only do calculations if there are locations
  14. if (mapLocations.length > 0) {
  15. let allLatitudes = R.map((l) => {
  16. if (l.latitude && !l.latitude.isNaN) return l.latitude
  17. }, mapLocations)
  18. let allLongitudes = R.map((l) => {
  19. if (l.longitude && !l.longitude.isNaN) return l.longitude
  20. }, mapLocations)
  21. let minLat = R.reduce(R.min, Infinity, allLatitudes)
  22. let maxLat = R.reduce(R.max, -Infinity, allLatitudes)
  23. let minLong = R.reduce(R.min, Infinity, allLongitudes)
  24. let maxLong = R.reduce(R.max, -Infinity, allLongitudes)
  25. let middleLat = (minLat + maxLat) / 2
  26. let middleLong = (minLong + maxLong) / 2
  27. let latDelta = (maxLat - minLat) + latPadding
  28. let longDelta = (maxLong - minLong) + longPadding
  29. // return markers
  30. return {
  31. latitude: middleLat,
  32. longitude: middleLong,
  33. latitudeDelta: latDelta,
  34. longitudeDelta: longDelta
  35. }
  36. }
  37. }