【发布时间】:2023-03-06 17:41:01
【问题描述】:
我正在创建一个窗口窗体应用程序,它使用谷歌 API 通过它们的 Lat-Long 计算两个位置之间的距离。这很好用,但我注意到有时 api 会为相同的位置给出不同的结果(距离)。
示例:-
位置-A => 孟买 (19.075983 , 72.877655)
位置-B => 德里 (28.704060 , 77.102493)
当我计算从 location-A 到 location-B
的距离时decimal Distance = EmployeeTaskWorkLogManager.getDistance(string.Concat(19.075983, ", ", 72.877655), string.Concat(28.704060, ", ", 77.102493));
这表明 距离 = 1423.297
当我计算从 location-B 到 location-A
的距离时decimal Distance = EmployeeTaskWorkLogManager.getDistance(string.Concat(28.704060, ", ", 77.102493), string.Concat(19.075983, ", ", 72.877655));
这表明 距离 = 1415.239
这是我用来计算两个位置之间距离的代码,
public static decimal getDistance(string origin, string destination)
{
decimal num;
Thread.Sleep(1000);
decimal num1 = new decimal(0);
string[] strArrays = new string[] { "https://maps.googleapis.com/maps/api/distancematrix/json?origins=", origin, "&destinations=", destination, "&key=****" };
string str = string.Concat(strArrays);
JObject jObjects = JObject.Parse(EmployeeTaskWorkLogManager.fileGetContents(str));
try
{
num1 = (decimal)jObjects.SelectToken("rows[0].elements[0].distance.value");
num = num1 / new decimal(1000);
}
catch
{
num = num1;
}
return num;
}
我检查了我的代码的每一行,但无法找到为什么会出现这种差异。
【问题讨论】:
-
你在谷歌地图上查看这个距离
-
是的,现在我检查了谷歌地图,我震惊了谷歌地图也显示了差异。有人知道为什么谷歌地图会显示这种差异吗?
-
您不应在您的代码中在线发布您的私人 Google API 密钥...
标签: c# .net google-maps google-maps-api-3 google-maps-api-2