高德地图之c#后台获取一个或多个起点到单个终点的直线距离
首先我们需要一个控制台添加一个新Key(可使用服务选择Web服务,测试的时候IP白名单先不填);
直线距离是通过后台get方式请求API服务地址http://restapi.amap.com/v3/distance返回得到的
直接上代码:
1 public string GetdistancesTest() 2 { 3 //具体参数可参考官方文档http://lbs.amap.com/api/webservice/guide/api/direction#distance 4 var client = new WebClient(); 5 var origins = "116.481028,39.989643|114.481028,39.989643|115.481028,39.989643";//起点,多个起点之间使用|分隔 6 var destination = "114.465302,40.004717";//单个终点 7 var type = "0";//返回类型,0:直线距离1:驾车导航距离2:公交规划距离3:步行规划距离 8 var output = "JSON";//可选值:JSON,XML 9 var key = Key;//个人控制台含有web服务的key 10 string disApiurl = string.Format("http://restapi.amap.com/v3/distance?origins={0}&destination={1}&type={2}&output={3}&key={4}", origins, destination, type, output, key); 11 return client.DownloadString(disApiurl); 12 }
返回的JSON数据如下为
{"status":"1","info":"OK","infocode":"10000","results":[{"origin_id":"1","dest_id":"1","distance":"171904","duration":"0"},{"origin_id":"2","dest_id":"1","distance":"2148","duration":"0"},{"origin_id":"3","dest_id":"1","distance":"86636","duration":"0"}]}
可以明显看到得到包含我们需要的直线距离数据了(单位:米)