最近做的一个项目需要我计算两地的行车距离,而不是直线距离
那肯定要用到相关的地图API啊,我选了百度地图
我设定的传入的参数有两个,分别的是出发地和目的地的区(县)名
一开始用的是JS的SDK来计算,但是对网络要求有点高,网络稳定的时候能计算出来,不稳定就计算不出来了,通过看百度地图开发API文档,我选择了通过Http接口的方式
首先在网上找了一个封装好的HttpClientUtil
public class HttpClientUtil {
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
}
下面是我自己写的计算两地距离的Util:
public class CalculateDisUtil {
//origin:出发地地名,如:余杭区
//destination:目的地地名,如:黄浦区
//返回两地的行车距离,如:234.56公里
public static String distance(String origin, String destination){
Map<String, String> params = new HashMap<>();
String originDouble = HttpClientUtil.doGet("http://api.map.baidu.com/geocoder/v2/?output=json&ak=你的key&address=" + origin);
String desDouble = HttpClientUtil.doGet("http://api.map.baidu.com/geocoder/v2/?output=json&ak=你的key&address=" + destination);
com.alibaba.fastjson.JSONObject jsonObjectOri = com.alibaba.fastjson.JSONObject.parseObject(originDouble);
com.alibaba.fastjson.JSONObject jsonObjectDes = com.alibaba.fastjson.JSONObject.parseObject(desDouble);
String oriLng = jsonObjectOri.getJSONObject("result").getJSONObject("location").getString("lng");
String oriLat = jsonObjectOri.getJSONObject("result").getJSONObject("location").getString("lat");
System.out.println(oriLat+"|"+oriLng);
String desLng = jsonObjectDes.getJSONObject("result").getJSONObject("location").getString("lng");
String desLat = jsonObjectDes.getJSONObject("result").getJSONObject("location").getString("lat");
System.out.println(desLat+"|"+desLng);
params.put("output","json");
params.put("ak","你的key");
params.put("origins",oriLat+","+oriLng+"|"+oriLat+","+oriLng);
params.put("destinations",desLat+","+desLng+"|"+desLat+","+desLng);
String result = HttpClientUtil.doGet("http://api.map.baidu.com/routematrix/v2/driving", params);
JSONArray jsonArray = com.alibaba.fastjson.JSONObject.parseObject(result).getJSONArray("result");
String distanceString = jsonArray.getJSONObject(0).getJSONObject("distance").getString("text");
return distanceString;
}
}
关于上面请求url的参数,只要看一下”批量算路”的服务文档即可