百度接口API的使用

因公司项目需要,需要用到一些距离成本计算的功能和地图视图工具数据库可视化,当时也调查了很多地图开发者中心和一些国外的地图API,最终选择了百度地图和谷歌地图的api来实现项目需求,下边给大家分享一下。

百度地图

百度地图开放平台:http://lbsyun.baidu.com.
进入开发者中心,上边导航栏则是百度提供的各种接口和开发文档,并且有各个语言的代码实例。我们选用的web-api,要使用api,首先需要注册成为开发者,并申请一个密钥。
进入web-api.页面,选择获取密钥,创建一个应用,并选择自己所需要的api接口。
百度接口API的使用_第1张图片
现在我们可以使用百度地图api来为我们服务了!

以批量算路为例:

点击进入批量算路的服务文档页面,有对步行、骑行和驾车三种计算方式的详系api解释说明。
下面做个小例子:
距离计算都是根据两地经纬度通过一定的算法来获取,所以首先需要调用百度的地图地理编码接口,将地址(尽可能详细)编码成经纬度,在进行计算。
首先是一些需要用到的jar

必须包

import java.io.IOException;
import java.net.URI;
import java.util.Map;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

获取方法

public static void main(String[] args) {
		Map params = new HashMap();
		String originDouble = HttpClientUtil
				.doGet("http://api.map.baidu.com/geocoder/v2/?output=json&ak=自己的ak&address="
						+ "河南省郑州市二七广场");
		String desDouble = HttpClientUtil
				.doGet("http://api.map.baidu.com/geocoder/v2/?output=json&ak=自己的ak&address="
						+ "北京市长安街天安门");
		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");// 纬度值ֵ

		String desLng = jsonObjectDes.getJSONObject("result").getJSONObject("location").getString("lng");
		String desLat = jsonObjectDes.getJSONObject("result").getJSONObject("location").getString("lat");
		params.put("output", "json");//输出方式为json
		params.put("tactics", "11");//10不走高速11常规路线12 距离较短(考虑路况)13距离较短(不考虑路况)
		params.put("ak", "自己的ak);
		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);
		com.alibaba.fastjson.JSONArray jsonArray = com.alibaba.fastjson.JSONObject.parseObject(result)
				.getJSONArray("result");
		// 获取距离、米
		String text = jsonArray.getJSONObject(0).getJSONObject("distance").getString("text");
		String value = jsonArray.getJSONObject(0).getJSONObject("distance").getString("value");
		System.out.println("取值text:" + text);
		System.out.println("取值value:" + value);
	}

通过HttpClientUtil进行请求。再此奉上代码:

public class HttpClientUtil {

	public static String doGet(String url, Map param) {

		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();
			HttpGet httpGet = new HttpGet(uri);
			response = httpclient.execute(httpGet);
			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 param) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			HttpPost httpPost = new HttpPost(url);
			// if (param != null) {
			// List paramList = new ArrayList<>();
			// for (String key : param.keySet()) {
			// paramList.add(new BasicNameValuePair(key, param.get(key)));
			// }
			// UrlEncodedFormEntity entity = new
			// UrlEncodedFormEntity(paramList);
			// httpPost.setEntity(entity);
			// }
			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) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			HttpPost httpPost = new HttpPost(url);
			StringEntity entity = new StringEntity(json,
					ContentType.APPLICATION_JSON);
			httpPost.setEntity(entity);
			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;
	}
}

百度接口API的使用_第2张图片
百度接口API的使用_第3张图片
完成!!

你可能感兴趣的:(Map)