经纬度计算距离
sql(计算结果默认单位为米,这里保留一位小数)
#关联查询当被关联表有指定条件时,会导致主表查询结果不正确 select u.name,a.area,a.lat,a.lon,a.is_main,(st_distance (point (a.lat,a.lon),point(120.218405,30.251983) ) * 111195) AS distance from user u left join addr a on a.user_id=u.id where a.is_main=1 #建议使用 select u.name,(select round(st_distance (point(120.218405,30.251983),point (lat,lon) ) * 111195,1) from addr where user_id=u.id) AS distance
获取地图经纬度(纬度(lat)在前经度在后(lon))
http://api.map.baidu.com/lbsapi/getpoint/index.html
存储
谷歌建议存储decimal(10,6)
只需要精确到小数点后7位,精度就是1CM,因此,数据库保存经纬度采用 decimal(10,7) 即可
说明:经纬度计算距离实际会略小于位移