繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM