我正在使用 GoogleAPI 来获取行驶里程和行驶距离。这是我的源代码...
dsAPI.ReadXml("http://maps.googleapis.com/maps/api/directions/xml?origin=" + strBaseLat + "," + strBaseLong + "&destination=" + strDStrLat + "," + strDStrLong + "&sensor=false");
Thread.Sleep(3000);
编辑代码:
dsAPI.ReadXml("http://maps.googleapis.com/maps/api/directions/xml?origin=" + strBaseLat + "," + strBaseLong + "&destination=" + strDStrLat + "," + strDStrLong + "&sensor=false");
Thread.Sleep(3000);
if (dsAPI.Tables[0].Rows.Count > 0)
{
status = dsAPI.Tables[0].Rows[0]["status"].ToString();
}
if (status == "OK")
{
if (dsAPI.Tables[2].Rows.Count > 0)
{
legid = Convert.ToInt32(dsAPI.Tables[2].Rows[0]["leg_Id"].ToString());
}
if (dsAPI.Tables[7].Rows.Count > 0)
{
DataView dvMinutes = new DataView(dsAPI.Tables[7]);
dvMinutes.RowFilter = "leg_Id = '" + legid + "'";
decMinutes = Convert.ToDecimal(dvMinutes[0]["value"] as string);
Min = decMinutes / 60;
Min = decimal.Round(Min, 2, MidpointRounding.AwayFromZero);
}
if (dsAPI.Tables[8].Rows.Count > 0)
{
DataView dvMiles = new DataView(dsAPI.Tables[8]);
dvMiles.RowFilter = "leg_Id = '" + legid + "'";
intMiles = Convert.ToInt32(dvMiles[0]["value"] as string);
Mil = Convert.ToDecimal(intMiles / 1609.34);
Mil = decimal.Round(Mil, 2, MidpointRounding.AwayFromZero);
}
API 将根据经纬度返回详细信息。在下一行中,我给出了 Thread.sleep。
如果我给thread.sleep,代码的剩余部分是否会等待,直到服务返回结果。我已经签入了我的代码,有时剩下的部分在没有等待结果的情况下被执行。
有没有其他选择。