# 高德地图 计算两经纬度 之间距离 lua实现

c#代码
private const double EARTH_RADIUS = 6378.137; //地球半径
{
return d * Math.PI / 180.0;
}

public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a/2),2) +
s = Math.Round(s * 10000) / 10000;
return s;
}

local start = {longitude=116.368904, latitude=39.923423}
local tail = {longitude=116.387271, latitude=39.922501}

https://lbs.amap.com/api/javascript-api/example/calcutation/calculate-distance-between-two-markers/

https://blog.csdn.net/jianggujin/article/details/72833711

function TestLua:CalDistance()
local start = {longitude=116.368904, latitude=39.923423}
local tail = {longitude=116.387271, latitude=39.922501}
print("[gps距离]"..tostring(self:CalculateLineDistance(start, tail)))
end

function TestLua:CalculateLineDistance(start, tail)
local d1 = 0.01745329251994329;
local d2 = start.longitude;
local d3 = start.latitude;
local d4 = tail.longitude;
local d5 = tail.latitude;
d2 = d2*d1;
d3 = d3*d1;
d4 = d4*d1;
d5 = d5*d1;
local d6 = math.sin(d2);
local d7 = math.sin(d3);
local d8 = math.cos(d2);
local d9 = math.cos(d3);
local d10 = math.sin(d4);
local d11 = math.sin(d5);
local d12 = math.cos(d4);
local d13 = math.cos(d5);
local arrayOfDouble1 = {};
local arrayOfDouble2 = {};
arrayOfDouble1[0]=(d9 * d8);
arrayOfDouble1[1]=(d9 * d6);
arrayOfDouble1[2]=(d7);

arrayOfDouble2[0]=(d13 * d12);
arrayOfDouble2[1]=(d13 * d10);
arrayOfDouble2[2]=(d11);
local d14 = math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) +
(arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) +
(arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
return(math.asin(d14 / 2.0) * 12742001.579854401);
end

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120