You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * @Author: Caven
  3. * @Date: 2020-04-23 09:29:56
  4. * @Last Modified by: Caven
  5. * @Last Modified time: 2020-05-12 00:46:48
  6. */
  7. export default function bounds(positions = [], expand = 0) {
  8. let minLng = 180
  9. let minLat = 90
  10. let maxLng = -180
  11. let maxLat = -90
  12. positions.forEach(item => {
  13. minLng = Math.min(minLng, item.lng || item.x)
  14. minLat = Math.min(minLat, item.lat || item.y)
  15. maxLng = Math.max(maxLng, item.lng || item.x)
  16. maxLat = Math.max(maxLat, item.lat || item.y)
  17. })
  18. if (expand > 0) {
  19. let diffLng = Math.abs(maxLng - maxLng)
  20. let diffLat = Math.abs(maxLat - minLat)
  21. minLng -= diffLng * expand
  22. minLat -= diffLat * expand
  23. maxLng += diffLng * expand
  24. maxLat += diffLat * expand
  25. }
  26. return {
  27. west: minLng,
  28. south: minLat,
  29. east: maxLng,
  30. north: maxLat
  31. }
  32. }