首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何发布Google距离矩阵参数?

如何发布Google距离矩阵参数?
EN

Stack Overflow用户
提问于 2012-11-19 11:58:58
回答 1查看 2K关注 0票数 0

谷歌距离矩阵可以接受多达100个参数。但是随着GET Ruquest url的长度限制在< 15 (我想2048个字符),我比我得到的错误414 -The请求的网址是太大,无法处理。因此,我得出结论,有必要使用POST方法。但我做不到。我找到REQUEST_DENIED eror了。那么我如何使用这个服务呢?

代码语言:javascript
复制
    public static bool GetMatrix(string origins, string destinations)
    {
        string poststring = string.Format("origins={0}&destinations={1}&mode=bicycling&language=fr-FR&sensor=false", origins, destinations);
        byte[] postdata = Encoding.UTF8.GetBytes(poststring);

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/xml");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/xml"; // or any other type dont work
        webRequest.ContentLength = postdata.Length;
        using (Stream writer = webRequest.GetRequestStream())
            writer.Write(postdata, 0, postdata.Length);

        using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
        {
            //Only for debug
            using (var stream = new StreamReader(webResponse.GetResponseStream()))
                System.Diagnostics.Debug.WriteLine(stream.ReadToEnd());

            return (webResponse.StatusCode == HttpStatusCode.OK);
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2012-11-19 12:19:36

JS方法

距离矩阵实例

代码语言:javascript
复制
var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = "Greenwich, England";
var destinationA = "Stockholm, Sweden";
var destinationB = new google.maps.LatLng(50.087692, 14.421150);

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
  {
    origins: [origin1, origin2],
    destinations: [destinationA, destinationB],
    travelMode: google.maps.TravelMode.DRIVING,
    avoidHighways: false,
    avoidTolls: false
  }, callback);

function callback(response, status) {
  // See Parsing the Results for
  // the basics of a callback function.
}

不需要C# 的距离计算方法

代码语言:javascript
复制
 public decimal calcDistance(decimal latA, decimal longA, decimal latB, decimal longB)
    {

        double theDistance = (Math.Sin(DegreesToRadians(latA)) *
                Math.Sin(DegreesToRadians(latB)) +
                Math.Cos(DegreesToRadians(latA)) *
                Math.Cos(DegreesToRadians(latB)) *
                Math.Cos(DegreesToRadians(longA - longB)));

        return Convert.ToDecimal((RadiansToDegrees(Math.Acos(theDistance)))) * 69.09M * 1.6093M;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13453343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档