zoukankan      html  css  js  c++  java
  • 百度驾车距离以及通过经纬度获取地址

    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 }
  • 相关阅读:
    RabbitMQ学习之spring-amqp的重要类的认识
    RabbitMQ学习之ConntectionFactory与Conntection的认知
    (转) RabbitMQ学习之远程过程调用(RPC)(java)
    (转) RabbitMQ学习之延时队列
    (转)RabbitMQ学习之消息可靠性及特性
    (转)RabbitMQ学习之exchange总结
    (转)RabbitMQ学习之Headers交换类型(java)
    心得体悟帖---200223(你决定玩游戏的那一刻开始,你就要做好花费大几个小时的准备)
    范仁义css3课程---44、弹性盒子(flex)实例
    心得体悟帖---200223(视频变短,于录课于观众都是好事)
  • 原文地址:https://www.cnblogs.com/zhanglingbing/p/10974976.html
Copyright © 2011-2022 走看看