我正在尝试通过使用 c# 在 Web 服务中调用 Google Map API XML 来获取源和目的地之间的方向详细信息。当我尝试在本地调用以下函数时,它工作正常。但是当我在服务器上部署我的代码时,对于某些位置,它没有提供方向详细信息。我尝试的本地系统是 Win2K8 R2,Web 服务器是 Win2K3。这是我的代码
public List<DirectionSteps> getDistance(string sourceLat, string sourceLong, string destLat, string destLong)
{
var requestUrl = String.Format("http://maps.google.com/maps/api/directions/xml?origin=" + sourceLat + "," + sourceLong + "&destination=" + destLat + "," + destLong + "&sensor=false&units=metric");
try
{
var client = new WebClient();
var result = client.DownloadString(requestUrl);
//return ParseDirectionResults(result);
var directionStepsList = new List<DirectionSteps>();
var xmlDoc = new System.Xml.XmlDocument { InnerXml = result };
if (xmlDoc.HasChildNodes)
{
var directionsResponseNode = xmlDoc.SelectSingleNode("DirectionsResponse");
if (directionsResponseNode != null)
{
var statusNode = directionsResponseNode.SelectSingleNode("status");
if (statusNode != null && statusNode.InnerText.Equals("OK"))
{
var legs = directionsResponseNode.SelectNodes("route/leg");
foreach (System.Xml.XmlNode leg in legs)
{
//int stepCount = 1;
var stepNodes = leg.SelectNodes("step");
var steps = new List<DirectionStep>();
foreach (System.Xml.XmlNode stepNode in stepNodes)
{
var directionStep = new DirectionStep();
directionStep.Index = stepCount++;
directionStep.Distance = stepNode.SelectSingleNode("distance/text").InnerText;
directionStep.Duration = stepNode.SelectSingleNode("duration/text").InnerText;
directionStep.Description = Regex.Replace(stepNode.SelectSingleNode("html_instructions").InnerText, "<[^<]+?>", "");
steps.Add(directionStep);
}
var directionSteps = new DirectionSteps();
//directionSteps.OriginAddress = leg.SelectSingleNode("start_address").InnerText;
//directionSteps.DestinationAddress = leg.SelectSingleNode("end_address").InnerText;
directionSteps.TotalDistance = leg.SelectSingleNode("distance/text").InnerText;
directionSteps.TotalDuration = leg.SelectSingleNode("duration/text").InnerText;
directionSteps.Steps = steps;
directionStepsList.Add(directionSteps);
}
}
}
}
return directionStepsList;
}
catch (Exception ex)
{
throw ex;
}
}