MySQL计算坐标点之间距离

11 篇文章 0 订阅
#计算两坐标点之间距离,单位米

-- 经度相同,维度不同,结果大致相等
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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值