![](/img/trans.png)
[英]Downloading a file in a C# Ajax application does not work when deployed to server. Running locally is fine and has no issues
[英]Google Map Direction Api Works Fine Locally but does not work properly when deployed on Server
我正在嘗試通過使用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;
}
}
在閱讀了許多帖子和Google使用政策后,事實證明Google不允許從FQDN或任何公共服務器進行此類自動查詢。 我提出了大約15-20個方向請求,在大約10個請求后被阻止。 我不得不改變我的邏輯,並在移動設備中實現了相同的功能,並從移動電話中調用了Google Maps Directions API,它的運行效果非常好! 看來Google並未阻止來自移動設備的此類請求。 但是您永遠都不知道他們何時將其改回。
您可以使用Google Maps API http://maps.googleapis.com/maps/api/directions/
這里的說明: https : //developers.google.com/maps/documentation/directions/
限制:
每天2,500個路線請求。
Google Maps API for Business客戶有更高的限制:
每天有100,000個路線請求。
每個請求中允許有23個航路點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.