經緯度計算距離
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) 即可
說明:經緯度計算距離實際會略小於位移