百度驾车距离以及通过经纬度获取地址
appkey 使用自己的
1 private final static String DRIVING = "http://api.map.baidu.com/routematrix/v2/driving?output=json&tactics=12&ak="; 2 3 4 private static String getDistance(String appkey,String fromLat, String fromLng,String toLat, String toLng) throws IOException { 5 StringBuilder result = new StringBuilder(); 6 URL url = new URL(getURL(appkey,fromLat, fromLng, toLat,toLng)); 7 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 8 conn.setDoInput(true); 9 conn.setDoOutput(true); 10 conn.setUseCaches(false); 11 conn.connect(); 12 BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); 13 String str = null; 14 while ((str = bReader.readLine()) != null) { 15 result.append(str); 16 } 17 bReader.close(); 18 conn.disconnect(); 19 return result.toString(); 20 } 21 22 private static String getURL(String appkey,String fromLat, String fromLng,String toLat, String toLng) { 23 StringBuilder url = new StringBuilder(DRIVING); 24 url.append(appkey); 25 if (!fromLat.equals("") && !fromLng.equals("")) { 26 url.append("&origins="); 27 url.append(new StringBuilder(fromLat).append(",").append(new StringBuilder(fromLng))); 28 } 29 if (!toLat.equals("") && !toLng.equals("")) { 30 url.append("&destinations="); 31 url.append(new StringBuilder(toLat).append(",").append(new StringBuilder(toLng))); 32 } 33 return url.toString(); 34 } 35 36 /*** 37 * @Author 38 * @Description 获取百度驾车时间与距离 39 **/ 40 public static Map<String,Integer> resultToMap(String appkey,String fromLat, String fromLng,String toLat, String toLng) throws IOException{ 41 String result = getDistance(appkey, fromLat, fromLng, toLat, toLng); 42 Map<String,Integer> map = new HashMap<>(); 43 int status = (int)new JSONObject(result).get("status"); 44 if (status == 0){//成功 45 map.put("status",0); 46 JSONObject object = new JSONObject(result).getJSONArray("result").getJSONObject(0); 47 System.out.println(object.getJSONObject("duration").get("value")); 48 int duration = (int)object.getJSONObject("duration").get("value"); 49 //持续时间(单位是s) 50 map.put("duration",duration); 51 //距离(单位是m) 52 int distance = (int)object.getJSONObject("distance").get("value"); 53 map.put("distance",distance); 54 }else {//失败 55 map.put("status",1); 56 } 57 return map; 58 } 59 60 /*** 61 * @Author 62 * @Description 通过经纬度获取地址 63 **/ 64 public static String getLocationInfo(String appkey, String lat, String lng) { 65 String url = "http://api.map.baidu.com/geocoder/v2/?location=" + lat + "," 66 + lng + "&output=json&ak=" + appkey + "&pois=0"; 67 String s = HttpRequestUtil.get(url); 68 if (StringUtils.isEmpty(s) || s.equals("请求失败")) { 69 return null; 70 } 71 com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(s); 72 String adress = ""; 73 if (jsonObject != null) { 74 Integer status = jsonObject.getInteger("status"); 75 if (status == 0) { 76 adress = jsonObject.getJSONObject("result").getString("formatted_address"); 77 } 78 } 79 return adress; 80 }