简体   繁体   English

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

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

相关问题 部署到服务器后,无法在C#Ajax应用程序中下载文件。 在本地运行很好,没有问题 - Downloading a file in a C# Ajax application does not work when deployed to server. Running locally is fine and has no issues Google Content API for Shopping-凭据在本地可以正常使用,但不能在实时服务器上使用 - Google Content API for Shopping - Credentials work fine locally but not on live server C# Azure ServiceBus / Azure SQL - 在本地工作,部署到 Azure 时似乎不起作用 - C# Azure ServiceBus / Azure SQL - works locally, does not seem to work when deployed to Azure 将项目部署到暂存时,System.IO.FileNotFoundException在本地运行良好 - System.IO.FileNotFoundException when project deployed to staging, works fine locally Google Calendar Api在本地运行良好,但未在服务器上提高其身份验证 - Google Calendar Api working fine Locally but not raising its Authentication on Server 本地站点可以正常工作,但是必须在兼容性设置中远程(部署)它 - locally site works fine, but remotely (deployed) it has to be included in the compatibility settings Database First DBContext在本地工作,但在部署时无法工作 - Database First DBContext works locally but not when deployed JavaScript在本地运行,但在部署到Web服务器时无法运行 - JavaScript works locally but not when deployed to webserver 简单的WebClient在本地工作,但在部署到Azure时无法工作 - Simple WebClient works locally, but not when deployed to Azure TLS在服务器盒中失败,但在本地工作正常 - TLS fails in server box but works fine locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM