最近接到一个需求获取当前用户的经纬度,然后计算与目标地的的距离,我自己也是看别人的博客学习,自己也做个记录吧
直接放出计算的公式,不想浪费时间的,直接看公式套进去就成.
--依次是纬度,纬度,经度
round((6378.138 * 2 * ASIN(SQRT(POW(SIN(( 纬度* PI( ) / 180 - latitude * PI( ) / 180 ) / 2 ), 2 ) + COS( 纬度* PI( ) / 180 ) *COS( latitude * PI( ) / 180 )*POW(SIN( ( 经度* PI( ) / 180 - longitude * PI( ) / 180 ) / 2 ), 2 ) ) ) * 1000 ))
---------------------下面是测试过程---------------------
1. 建表测试
说明:这里都是百度的经纬度,腾讯地图和百度地图的经纬度是不同的.
-- 先建一张表
drop table if exists test;
create table test(
id Int not null auto_increment comment "主键",
city varchar(20) comment "城市",
address varchar(20) comment"地址",
longitude decimal(10,7) comment '经度',
latitude decimal(10,7) comment '纬度',
primary key (id)
);
-- 插入数据
insert into test(city,address,longitude,latitude) values('北京','北京市','116.395645','39.929986');
insert into test(city,address,longitude,latitude) values('深圳','广东省深圳市','114.025974','22.546054');
insert into test(city,address,longitude,latitude) values('上海','上海市','121.487899','31.249162');
insert into test(city,address,longitude,latitude) values('郑州','河南省郑州市','113.649644','34.75661');
--计算 这里以南京市为起点
SELECT
*,
round (
(
6378.138 * 2 * ASIN(
SQRT(
POW(
SIN( ( 32.057236 * PI( ) / 180 - t1.latitude * PI( ) / 180 ) / 2 ),
2
) + COS( 32.057236 * PI( ) / 180 ) * COS( t1.latitude * PI( ) / 180 ) * POW(
SIN( ( 118.778074 * PI( ) / 180 - t1.longitude * PI( ) / 180 ) / 2 ),
2
)
)
) * 1000
)
) AS '距离(单位是米)'
from test t1
2. 测试结果
3.总结
3.1 首先这种方式计算的并不准确,多少还是存在误差的,不过也是一种解决办法;
3.2 这种办法确实挺low的,目前还没有特别好的解决办法;
4.其他补充
4.1目前我国的经纬度范围
最东i端 东经135度2分30秒 黑龙江和乌苏里江交汇处
最西端 东经73度40分 帕米尔高原乌兹别里山口(乌恰县)
最南端 北纬3度52分 南沙群岛曾母暗沙
最北端 北纬53度33分 漠河以北黑龙江主航道(漠河)
4.2 来补一补经纬度课吧------->如何辨别地理中的经纬度详解
4.3 附一份百度地图的城市中心经纬度----->百度地图城市中心点经纬度