Google Map Direction Api在本地工作正常但在服务器上部署时无法正常工作

时间:2013-05-26 18:28:59

标签: c# latitude-longitude direction

我试图通过使用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; 
    }

    }

2 个答案:

答案 0 :(得分:0)

在阅读了很多帖子和Google使用政策之后,事实证明Google不允许来自FQDN或任何公共服务器的此类自动查询。我正在制作大约15-20个方向请求,在大约10个请求后被阻止。我不得不改变我的逻辑,并在移动设备中实现相同的功能,并从移动设备调用谷歌地图方向api,它的工作完美!谷歌似乎没有阻止此类请求来自移动设备。但你永远都不知道他们何时改变它。

答案 1 :(得分:0)

您可以使用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个航路点。