[英]Google Map Direction Api Works Fine Locally but does not work properly when deployed on Server
I am trying to get direction details between a source and destination by calling Google Map API XML in a web service using c#. 我正在尝试通过使用c#在Web服务中调用Google Map API XML来获取源与目的地之间的路线详细信息。 When I try to call below function locally, it works fine.
当我尝试在本地调用以下函数时,它可以正常工作。 But when I deploy my code on the server, for some locations, it does not give direction details.
但是,当我在服务器上的某些位置部署代码时,它没有提供方向详细信息。 The local System where I try is Win2K8 R2 and the web server is Win2K3.
我尝试的本地系统是Win2K8 R2,Web服务器是Win2K3。 Here is my Code
这是我的代码
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;
}
}
After reading many posts and google usage policy, it turns out that Google does not allow such automated queries from FQDN or any public server. 在阅读了许多帖子和Google使用政策后,事实证明Google不允许从FQDN或任何公共服务器进行此类自动查询。 I was making around 15-20 direction request which was blocked after around 10 requests.
我提出了大约15-20个方向请求,在大约10个请求后被阻止。 I had to change my logic and implemented the same function in mobile device and called the google maps directions api from mobile and it works perfectly !!
我不得不改变我的逻辑,并在移动设备中实现了相同的功能,并从移动电话中调用了Google Maps Directions API,它的运行效果非常好! It seems that google is not blocking such requests when they come from mobile devices.
看来Google并未阻止来自移动设备的此类请求。 But you never know when they change it back.
但是您永远都不知道他们何时将其改回。
You can use Google maps api http://maps.googleapis.com/maps/api/directions/ 您可以使用Google Maps API http://maps.googleapis.com/maps/api/directions/
Instaructions here: https://developers.google.com/maps/documentation/directions/ 这里的说明: https : //developers.google.com/maps/documentation/directions/
Limits: 限制:
2,500 directions requests per day.
每天2,500个路线请求。
Google Maps API for Business customers have higher limits:
Google Maps API for Business客户有更高的限制:
100,000 directions requests per day.
每天有100,000个路线请求。
23 waypoints allowed in each request.
每个请求中允许有23个航路点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.