java后台调用百度API计算两点驾车距离

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.log4j.Logger;




import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**   
 * @ClassName: BaiduAPI
 * @Description: 调用百度API得到相应数据的方法类
 * @author yangyanqiu
 * @date 2017-8-29 上午09:03:29
 */
public class BaiduAPI {

// 记录日志
static Logger logger = Logger.getLogger(BaiduAPI.class);

/**
* @Title: getDistanceByPoints
* @Description: 调用百度API接口计算两点驾车距离
* @return 返回map
*/
@SuppressWarnings("unchecked")
public static Map getDistanceByPoints(String startPoint,String endPoint){

//存放返回值map
Map map = new HashMap();


Map startPointMap = JsonUtil.getMap4Json(startPoint);
Map endPointMap = JsonUtil.getMap4Json(endPoint);

//拼接计算两点驾车距离的百度API接口
String url =  String.format("http://api.map.baidu.com/routematrix/v2/driving?"+
"output=json&origins=" + Double.parseDouble(StringUtil.convertMapString(startPointMap, "lat"))+
"," + Double.parseDouble(StringUtil.convertMapString(startPointMap, "lng"))+
"&destinations="+Double.parseDouble(StringUtil.convertMapString(endPointMap, "lat"))+
"," + Double.parseDouble(StringUtil.convertMapString(endPointMap, "lng"))+
"&ak=你的秘钥");

String json = loadJSON(url);  
 
//将返回结果转换成map
Map reMap = JsonUtil.getMap4Json(json);

      logger.info("请求数据:  "+reMap);
     
      //请求成功
      if(StringUtil.convertMapString(reMap, "status").equals("0")){


      Map resultMap = (Map) ((List) (reMap.get("result"))).get(0);
      Map disMap = (Map)resultMap.get("distance");
      Map timeMap = (Map)resultMap.get("duration");
     
      String distanceStr = StringUtil.convertMapString(disMap, "text");
      Double distanceValue = Double.parseDouble(StringUtil.convertMapString(disMap, "value"));
      String timeStr = StringUtil.convertMapString(timeMap, "text");
      Double time = Double.parseDouble(StringUtil.convertMapString(timeMap, "value"));
     
      map.put("DISTANCE_TEXT", distanceStr);
      map.put("DISTANCE", distanceValue);
      map.put("TIME_TEXT", timeStr);
      map.put("TIME", time);
      }
return map;
}


 /**
* @Title: loadJSON
* @Description: 得到请求返回结果
* @param url 百度API请求地址
* @return 返回String雷锤
*/
public static String loadJSON (String url) {  
          StringBuilder json = new StringBuilder();  
          try {  
              URL oracle = new URL(url);  
              URLConnection yc = oracle.openConnection();  
              BufferedReader in = new BufferedReader(new InputStreamReader(  
                                          yc.getInputStream()));  
              String inputLine = null;  
              while ( (inputLine = in.readLine()) != null) {  
                  json.append(inputLine);  
              }  
              in.close();  
          } catch (MalformedURLException e) {  
         e.printStackTrace();
          } catch (IOException e) {  
         e.printStackTrace();
          }  
          return json.toString();  
      }  

public static void main(String[] args){
String point1 = "{'lng':117.14566177091,'lat':31.842170749583}";

String point2 = "{'lng':117.14555178002,'lat':31.84196792998}";

Map map = BaiduAPI.getDistanceByPoints(point1, point2);


System.out.println("distanceText:   " + map.get("DISTANCE_TEXT"));

  System.out.println("value:   " + map.get("DISTANCE"));
}
}

你可能感兴趣的:(java后台调用百度API计算两点驾车距离)