通过SQL语句计算两地的经纬度距离

博客主要介绍了SQL案例及参数说明,代码用于计算两地经纬度距离,对相关参数进行了解释。还给出成功案例演示,与腾讯地图对比,取当前定位和数据库第一条数据测试,测试结果基本一致,计算后的距离返回后台可保留两位小数。

一、SQL案例

select se.EnterpriseId,se.Code,se.Type,se.Name,se.UnifiedCode,se.Latitude,se.Longitude
	,se.Address,se.RedBlackList,se.StatusOfBiz,se.IsQualifyM,se.IsQualify4S,se.IsChainStore
	,se.IsMainStore,se.MainStoreName,se.ContactsPhone,se.BizCategory,se.GoodCount
	,se.EnterpriseRegistStatus
	#通过经纬度计算距离
    ,ACOS(
         SIN( #{enterprise.latitude} * PI() / 180 ) * SIN(
         cast(
              if((se.Latitude is null or ltrim(rtrim(se.Latitude)) = ''), 0.0, se.Latitude) 
              AS DECIMAL ( 12, 8 )) * PI() / 180
             ) + COS(#{enterprise.latitude}  * PI() / 180 ) * COS(
                     cast(
                     	if((se.Latitude is null or ltrim(rtrim(se.Latitude)) = ''), 0.0, 
                     		se.Latitude) AS DECIMAL ( 12, 8 )) * PI() / 180
                      	) * COS(
                   	 #{enterprise.longitude}  * PI() / 180 - cast(
                   	if((se.Longitude is null or ltrim(rtrim(se.Longitude)) = ''), 0.0, 
                   		se.Longitude) AS DECIMAL ( 12, 8 )) * PI() / 180
                   )
                ) * 6378.14 * 1000 as Distance
	from sys_enterprise as se
	where se.IsEnabled = 1
	and se.IsAbolish = 0
	and se.Type = 0
	and se.EnterpriseId = #{enterprise.enterpriseId}

二、SQL参数说明

#通过经纬度计算距离
    ,ACOS(
         SIN( #{enterprise.latitude} * PI() / 180 ) * SIN(
         cast(
              if((se.Latitude is null or ltrim(rtrim(se.Latitude)) = ''), 0.0, se.Latitude) 
              AS DECIMAL ( 12, 8 )) * PI() / 180
             ) + COS(#{enterprise.latitude}  * PI() / 180 ) * COS(
                     cast(
                     	if((se.Latitude is null or ltrim(rtrim(se.Latitude)) = ''), 0.0, 
                     		se.Latitude) AS DECIMAL ( 12, 8 )) * PI() / 180
                      	) * COS(
                   	 #{enterprise.longitude}  * PI() / 180 - cast(
                   	if((se.Longitude is null or ltrim(rtrim(se.Longitude)) = ''), 0.0, 
                   		se.Longitude) AS DECIMAL ( 12, 8 )) * PI() / 180
                   )
                ) * 6378.14 * 1000 as Distance

这一块代码用来计算两地的经纬度距离!
参数说明:
(1):#{enterprise.latitude} = 纬度
(2):#{enterprise.longitude} =经度

成功案例演示(对比腾讯地图):

代码案例

#查询当前定位附近维修企业距离(当前定位经纬度:22.53332,113.93041)
select se.Code,se.Name,se.Latitude as '企业纬度',se.Longitude as '企业经度',ACOS(
         SIN(22.53332 * PI() / 180 ) * SIN(
         cast(
              if((se.Latitude is null or ltrim(rtrim(se.Latitude)) = ''), 0.0, se.Latitude) 
              AS DECIMAL ( 12, 8 )) * PI() / 180
             ) + COS(22.53332  * PI() / 180 ) * COS(
                     cast(
                     	if((se.Latitude is null or ltrim(rtrim(se.Latitude)) = ''), 0.0, 
                     		se.Latitude) AS DECIMAL ( 12, 8 )) * PI() / 180
                      	) * COS(
                   	 113.93041  * PI() / 180 - cast(
                   	if((se.Longitude is null or ltrim(rtrim(se.Longitude)) = ''), 0.0, 
                   		se.Longitude) AS DECIMAL ( 12, 8 )) * PI() / 180
                   )
                ) * 6378.14 * 1000 as Distance
	
from sys_enterprise as se
order by Distance asc

执行结果:
在这里插入图片描述
Distance就是计算经纬度后的距离,返回给后台后可以自己保留二位小数!
取当前定位和数据库第一条数据测试(腾讯地图接口返回)
在这里插入图片描述
测试结果,基本一致!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值