#计算两坐标点之间距离,单位米
-- 经度相同,维度不同,结果大致相等
select st_distance_sphere(point(122.45,31.13), point(122.45, 31.93)) as distance;
select st_distance(point(122.45,31.13), point(122.45, 31.93)) * 111195 as distance;
-- 经度不同,维度相同,结果差异较大
select st_distance_sphere(point(121.45,31.13), point(129.45, 31.13)) as distance;
select st_distance(point(121.45,31.13), point(129.45, 31.13)) * 111195 as distance;
-- 经度维度都不同,结果差异较大
select st_distance_sphere(point(121.45,31.13), point(129.45, 39.13)) as distance;
select st_distance(point(121.45,31.13), point(129.45, 39.13)) * 111195 as distance;
//java代码计算两坐标点之间距离
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>
public static Double calcGeodeticCurve(Double fromLatitude, Double fromLongitude, Double toLatitude, Double toLongitude) {
GlobalCoordinates source = new GlobalCoordinates(fromLatitude, fromLongitude);
GlobalCoordinates target = new GlobalCoordinates(toLatitude, toLongitude);
//对比百度地图,计算结果和Sphere坐标系计算结果一致,表明计算结果正确,WGS84坐标系的计算结果存在几十米的误差。
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target);
return geoCurve.getEllipsoidalDistance();
}
@Test
public void test2(){
Double distance = Utils.calcGeodeticCurve(31.13, 121.45, 39.13, 129.45);
System.out.println("距离:" + distance + "米");
}
//距离:1148121.7604524833米
有种说法:st_distance 计算的结果单位是度,需要乘111195(地球半径: 6371000*PI/180)将值转化为米才是坐标点之间距离。这种说法不够准确,每个坐标点所在维度不同,每度所代表的长度也是不同的,只有在经度相同维度不同时结果才是大致准确的。
mysql 5.7之后又有了一个st_distance_sphere(point(x,y),point(x,y))函数来计算坐标点距离,这个就比较准确一些。
在线计算距离工具:GPS,经度,纬度,距离在线计算器,在线计算,在线计算器,计算器在线计算开源地理空间基金会中文分会实验室,关注开源GIS,开放数据https://www.osgeo.cn/app/s1884