Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /**
  4. * 角度转弧度
  5. */
  6. function toRadians (deg) {
  7. return deg / 180 * Math.PI;
  8. }
  9.  
  10. /**
  11. * 当给定的两位置相距较近时,可将经纬线近似为垂直,计算对角线近似为两点距离。
  12. */
  13. function getDistanceSimple (coordinate1, coordinate2) {
  14. const lat1 = coordinate1.latitude;
  15. const lat2 = coordinate2.latitude;
  16. const lng1 = coordinate1.longitude;
  17. const lng2 = coordinate2.longitude;
  18. const dx = lng1 - lng2; // 经度差值
  19. const dy = lat1 - lat2; // 纬度差值
  20. const b = (lat1 + lat2) / 2.0; // 平均纬度
  21. const Lx = toRadians(dx) * 6367000.0 * Math.cos(toRadians(b)); // 东西距离
  22. const Ly = 6367000.0 * toRadians(dy); // 南北距离
  23. return Math.hypot(Lx, Ly); // 用平面的矩形对角距离公式计算总距离
  24. }
  25.  
  26. module.exports = {
  27. getDistanceSimple
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement