百度地图 获取两点坐标之间的驾车距离(非直线距离) c#

时间:2022-06-01 19:37:42

百度接口了解:

?title=webapi/route-matrix-api-v2

起点与终点为多对多关系,如果你只想取两个坐标,,那就各取一个坐标即可。

如下方法,经过测试,获取的数据是正确的。方法没写完,自己转json数据

/// <summary> /// 根据坐标点获取驾车距离 /// </summary> /// <param>起点坐标</param> /// <param>终点坐标</param> /// <returns></returns> public static string GetDistance(string origins, string destinations) { try { //测试数据 origins = "22.823331,108.326998"; destinations = "22.832541,108.321788|22.83841,108.294974|22.817868,108.425637|22.883959,108.305368|22.83334,108.32637"; //行车距离 string url = "http://api.map.baidu.com/routematrix/v2/driving"; string ak = "595eRciHjA0MG4TmhTm5ak58M00bQand"; string param = string.Format("origins={0}&destinations={1}&output=json&ak={2}", origins, destinations, ak); string strURL = url + ‘?‘ + param; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); request.Method = "GET"; System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); string StrDate = ""; string strValue = ""; StreamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "\r\n"; } return strValue; } catch (Exception) { } return ""; }