C#版
public static double GetDistance(double long1, double lat1, double long2, double lat2) { double a, b, R; R = 6378137; //地球半径 lat1 = lat1 * Math.PI / 180.0; lat2 = lat2 * Math.PI / 180.0; a = lat1 - lat2; b = (long1 - long2) * Math.PI / 180.0; double d; double sa2, sb2; sa2 = Math.Sin(a / 2.0); sb2 = Math.Sin(b / 2.0); d = 2 * R * Math.Asin(Math.Sqrt(sa2 * sa2 + Math.Cos(lat1) * Math.Cos(lat2) * sb2 * sb2)); return d; }
sql语句版
$user_lat='41.749034'; //传过来的纬度 $user_lng='123.46017';//传过来的经度 $store_mod=db("store"); //数据库 $storelist=$store_mod->query("SELECT *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN((".$user_lat."*PI()/180-【store_latitude】*PI()/180)/2),2)+COS(".$user_lat."*PI()/180)*COS(【store_latitude】*PI()/180)*POW(SIN((".$user_lng."*PI()/180-【store_longitude】*PI()/180)/2),2)))*1000) AS distance FROM store HAVING 【搜索条件如 state=1】 distance<【周围距离多少米 如 500】 ORDER BY distance ASC"); SELECT *,ROUND(6378.138*2*ASIN(SQRT(POW(SIN((22.121700*PI()/180-stationinfo.lat*PI()/180)/2),2)+COS(22.121700*PI()/180)*COS(stationinfo.lat*PI()/180)* POW(SIN((99.285103*PI()/180-stationinfo.lng*PI()/180)/2),2)))*1000) AS distance FROM stationinfo ORDER BY distance ASC