根据经纬度坐标根据高德地图计算开车的实际路线距离

private static String getResponse(String serverUrl){
		System.out.println(serverUrl);
		try {
			URL url = new URL(serverUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true); // 设置该连接是可以输出的
			connection.setRequestMethod("GET"); // 设置请求方式
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
			String line = null;
			StringBuilder result = new StringBuilder();
			while ((line = br.readLine()) != null) { // 读取数据
				result.append(line + "\n");
			}
			connection.disconnect();
			return result.toString();//返回值
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String getDistance(String startLonLat, String endLonLat,String city,String cityd){
		String key = "你自己申请的key";
		//返回起始地startAddr与目的地endAddr之间的距离,单位:米
		String result = "";
		String queryUrl = "https://restapi.amap.com/v3/distance?origins="+startLonLat+"&destination="+endLonLat+"&output=xml&key="+key+"&city="+city+"&cityd="+cityd+"&strategy=0&nightflag=0";
		try {
			String queryResult = getResponse(queryUrl);
			System.out.println("++++++++++++++++++++++++++++++++++++++++++");
			System.out.println(queryResult);
			//截取距离长度结果值
			String str = queryResult.substring(queryResult.indexOf("<distance>")+10,queryResult.indexOf("</distance>"));
			System.out.println(str);
			return str;
		} catch (Exception e) {
			e.printStackTrace();
			return  "";
		}
	}

官方链接:https://lbs.amap.com/api/javascript-api/guide/services/navigation
对应参数解释如下图:
在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值