当前位置:首页 > 机动车 >

经纬度距离的计算方法(经纬度之间的距离计算方法)

来源:原点资讯(www.yd166.com)时间:2023-10-27 00:47:28作者:YD166手机阅读>>

  • Java实现
  • Javascript实现
  • MySQL实现

最近在做与地图相关的应用,使用了高德地图,研究了下高德地图计算两坐标距离的方法,官网上提供的开发包中有相关的方法,但是我的产品中比较特殊,无法直接使用提供的方法,所以就自己封装了相关计算方法,供大家参考。

Java实现

首先定义一个用于存储经纬度的类,这里起个名字叫:LngLat

package amap; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; /** * 存储经纬度坐标值的类,单位角度 * * @author jianggujin * */ public final class LngLat implements Cloneable { /** * 纬度 (垂直方向) */ public final double latitude; /** * 经度 (水平方向) */ public final double longitude; /** * 格式化 */ private static DecimalFormat format = new DecimalFormat("0.000000", new DecimalFormatSymbols(Locale.US)); /** * 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点。 * * @param longitude * 地点的经度,在-180 与180 之间的double 型数值。 * @param latitude * 地点的纬度,在-90 与90 之间的double 型数值。 */ public LngLat(double longitude, double latitude) { this(longitude, latitude, true); } /** * 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点 * * @param longitude * 地点的经度,在-180 与180 之间的double 型数值。 * * @param latitude * 地点的纬度,在-90 与90 之间的double 型数值。 * @param isCheck * 是否需要检查经纬度的合理性,建议填写true */ public LngLat(double longitude, double latitude, boolean isCheck) { if (isCheck) { if ((-180.0D <= longitude) && (longitude < 180.0D)) this.longitude = parse(longitude); else { throw new IllegalArgumentException("the longitude range [-180, 180]."); // this.longitude = parse(((longitude - 180.0D) % 360.0D 360.0D) % // 360.0D - 180.0D); } if ((latitude < -90.0D) || (latitude > 90.0D)) { throw new IllegalArgumentException("the latitude range [-90, 90]."); } this.latitude = latitude; // this.latitude = parse(Math.max(-90.0D, Math.min(90.0D, latitude))); } else { this.latitude = latitude; this.longitude = longitude; } } /** * 解析 * * @param d * @return */ private static double parse(double d) { return Double.parseDouble(format.format(d)); } public LngLat clone() { return new LngLat(this.latitude, this.longitude); } @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(latitude); result = prime * result (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(longitude); result = prime * result (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; LngLat other = (LngLat) obj; if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude)) return false; if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude)) return false; return true; } public String toString() { return "lat/lng: (" this.latitude "," this.longitude ")"; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

计算工具类如下:

package amap; /** * 高德地图工具 * * @author jianggujin * */ public class AMapUtils { /** * 根据用户的起点和终点经纬度计算两点间距离,此距离为相对较短的距离,单位米。 * * @param start * 起点的坐标 * @param end * 终点的坐标 * @return */ public static double calculateLineDistance(LngLat start, LngLat end) { if ((start == null) || (end == null)) { throw new IllegalArgumentException("非法坐标值,不能为null"); } double d1 = 0.01745329251994329D; double d2 = start.longitude; double d3 = start.latitude; double d4 = end.longitude; double d5 = end.latitude; d2 *= d1; d3 *= d1; d4 *= d1; d5 *= d1; double d6 = Math.sin(d2); double d7 = Math.sin(d3); double d8 = Math.cos(d2); double d9 = Math.cos(d3); double d10 = Math.sin(d4); double d11 = Math.sin(d5); double d12 = Math.cos(d4); double d13 = Math.cos(d5); double[] arrayOfDouble1 = new double[3]; double[] arrayOfDouble2 = new double[3]; arrayOfDouble1[0] = (d9 * d8); arrayOfDouble1[1] = (d9 * d6); arrayOfDouble1[2] = d7; arrayOfDouble2[0] = (d13 * d12); arrayOfDouble2[1] = (d13 * d10); arrayOfDouble2[2] = d11; double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2])); return (Math.asin(d14 / 2.0D) * 12742001.579854401D); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

最后边写一段测试代码测试一下:

package test; import org.junit.Test; import amap.AMapUtils; import amap.LngLat; public class AMapTest { @Test public void Test() { LngLat start = new LngLat(116.368904, 39.923423); LngLat end = new LngLat(116.387271, 39.922501); System.err.println(AMapUtils.calculateLineDistance(start, end)); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

运行结果为:1569.6213922679392,官网的javascript API示例结果如图:

结果虽然有一点误差,但是这hi在可接受范围内的。

Javascript实现

同样的算法,将其转换成JS的写法,完整的代码如下:

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script type="text/javascript" src="//img.yd166.com/js/ajax.js"></script> <script> /** * 存储经纬度 * @param {Object} longitude * @param {Object} latitude */ function LngLat(longitude, latitude) { this.longitude = longitude; this.latitude = latitude; } function calculateLineDistance(start, end) { var d1 = 0.01745329251994329; var d2 = start.longitude; var d3 = start.latitude; var d4 = end.longitude; var d5 = end.latitude; d2 *= d1; d3 *= d1; d4 *= d1; d5 *= d1; var d6 = Math.sin(d2); var d7 = Math.sin(d3); var d8 = Math.cos(d2); var d9 = Math.cos(d3); var d10 = Math.sin(d4); var d11 = Math.sin(d5); var d12 = Math.cos(d4); var d13 = Math.cos(d5); var arrayOfDouble1 = []; var arrayOfDouble2 = []; arrayOfDouble1.push(d9 * d8); arrayOfDouble1.push(d9 * d6); arrayOfDouble1.push(d7); arrayOfDouble2.push(d13 * d12); arrayOfDouble2.push(d13 * d10); arrayOfDouble2.push(d11); var d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2])); return(Math.asin(d14 / 2.0) * 12742001.579854401); } var start = new LngLat(116.368904, 39.923423); var end = new LngLat(116.387271, 39.922501); </script> </head> <body> <script> document.write(calculateLineDistance(start, end)); </script> </body> </html> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 MySQL实现

DELIMITER $$ CREATE FUNCTION `calculateLineDistance`(startLng double, startLat double, endLng double, endLat double) RETURNS double BEGIN declare d2 DOUBLE; declare d3 DOUBLE; declare d4 DOUBLE; declare d5 DOUBLE; declare d6 DOUBLE; declare d7 DOUBLE; declare d8 DOUBLE; declare d9 DOUBLE; declare d10 DOUBLE; declare d11 DOUBLE; declare d12 DOUBLE; declare d13 DOUBLE; declare d14 DOUBLE; declare arrayOfDouble10 DOUBLE; declare arrayOfDouble11 DOUBLE; declare arrayOfDouble12 DOUBLe; declare arrayOfDouble20 DOUBLE; declare arrayOfDouble21 DOUBLE; declare arrayOfDouble22 DOUBLE; set d2 = startLng * 0.01745329251994329; set d3 = startLat * 0.01745329251994329; set d4 = endLng * 0.01745329251994329; set d5 = endLat * 0.01745329251994329; set d6 = sin(d2); set d7 = sin(d3); set d8 = cos(d2); set d9 = cos(d3); set d10 = sin(d4); set d11 = sin(d5); set d12 = cos(d4); set d13 = cos(d5); set arrayOfDouble10 = (d9 * d8); set arrayOfDouble11 = (d9 * d6); set arrayOfDouble12 = d7; set arrayOfDouble20 = (d13 * d12); set arrayOfDouble21 = (d13 * d10); set arrayOfDouble22 = d11; set d14 = sqrt((arrayOfDouble10 - arrayOfDouble20) * (arrayOfDouble10 - arrayOfDouble20) (arrayOfDouble11 - arrayOfDouble21) * (arrayOfDouble11 - arrayOfDouble21) (arrayOfDouble12 - arrayOfDouble22) * (arrayOfDouble12 - arrayOfDouble22)); return (asin(d14 / 2.0) * 12742001.579854401); END $$ DELIMITER ;

经纬度距离的计算方法,经纬度之间的距离计算方法(1)

,

栏目热文

经纬度算距离计算器(经纬度转换为精确坐标计算器)

经纬度算距离计算器(经纬度转换为精确坐标计算器)

EIA环评计算器软件结构如下:水预测工具---生活污水和垃圾---暴雨强度和雨水量计算---S-P模式---二维稳态混合...

2023-10-27 01:12:48查看全文 >>

excel两个经纬度算距离公式(excel两个经纬度怎么算距离)

excel两个经纬度算距离公式(excel两个经纬度怎么算距离)

1 前言EXCEL是我们日常处理数据的办公软件,操作简单实用,能够快速的、批量的对表格数据进行处理。EXCEL自身不提供...

2023-10-27 01:33:39查看全文 >>

经纬度距离计算小工具(两个经纬度之间的距离计算方法)

经纬度距离计算小工具(两个经纬度之间的距离计算方法)

小程序业余无线电工具集为广大业余无线电爱好者提供一个综合查询类服务,具体涵盖业余无线电操作证习题练习与模拟考试、全国业余...

2023-10-27 01:04:24查看全文 >>

4006700700是腾讯号码吗(4006700700怎么转人工接听)

4006700700是腾讯号码吗(4006700700怎么转人工接听)

你知道你的手机号码绑定了哪些App吗?你的手机号码是否被人盗用注册过App?如何查询、解绑已经不常用的App账号?如果你...

2023-10-27 01:14:38查看全文 >>

托雷基亚(托雷基亚为什么选择了雾崎)

托雷基亚(托雷基亚为什么选择了雾崎)

托雷基亚为了保护泰迦,不惜一拳打飞贝利亚。就在泰迦陷入困境之时,赛罗及时赶到。不久前,在一座大厦里,有一群憎恨奥特战士的...

2023-10-27 00:58:35查看全文 >>

经纬度计算公式表(经纬度转换度数公式)

经纬度计算公式表(经纬度转换度数公式)

经纬度是经度与纬度的合称组成一个坐标系统,称为地理坐标系统,它是一种利用三度空间的球面来定义地球上的空间的球面坐标系统,...

2023-10-27 01:09:16查看全文 >>

经纬度的距离怎么算(经纬度怎么换算成距离)

经纬度的距离怎么算(经纬度怎么换算成距离)

点击上方【大水牛测绘】关注我们一、坐标正算根据直线始点的坐标、直线的水平距离及其方位角,计算直线终点的坐标,称为坐标正算...

2023-10-27 00:47:26查看全文 >>

经纬度实际长度计算公式(经纬度最佳计算方法)

经纬度实际长度计算公式(经纬度最佳计算方法)

1、坐标系投影方式的选择1.1高斯-克吕格投影高斯-克吕格(Gauss-Kruger)投影,简称高斯投影,是一种"等角横...

2023-10-27 01:02:01查看全文 >>

经纬度的距离计算公式怎么理解(经纬度和距离之间的计算关系)

经纬度的距离计算公式怎么理解(经纬度和距离之间的计算关系)

经纬度是某个地点的坐标信息,通过经纬度数据就可以计算出这两点之间的直线距离,PowerBI能不能实现这种计算呢?其实这是...

2023-10-27 00:56:21查看全文 >>

文档排行