百度api
因公司项目需要,需要用到一些距离成本计算的功能和地图视图工具数据库可视化,当时也调查了很多地图开发者中心和一些国外的地图API,最终选择了百度地图和谷歌地图的api来实现项目需求,下边给大家分享一下。
百度地图
百度地图开放平台:http://lbsyun.baidu.com.
进入开发者中心,上边导航栏则是百度提供的各种接口和开发文档,并且有各个语言的代码实例。我们选用的web-api,要使用api,首先需要注册成为开发者,并申请一个密钥。
进入web-api.页面,选择获取密钥,创建一个应用,并选择自己所需要的api接口。
现在我们可以使用百度地图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<String, String> params = new HashMap<String, String>();
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<String, String> 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<String, String> param) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
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);
// }
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;
}
}
完成!!
相关阅读
SD_SALESDOCUMENT_CREATE 用于创建销售订单。 创建销售订单的BAPI,比较常用的是BAPI_SALESORDER_CREATEFROMDAT2,但是 BUSINESS O
从百度糯米”517吃货节”,看脱离广告的营销
不久之后京东的“618”广告又将铺天盖地席卷而来,为了与天猫“双十一”对标,向巨头宣战,京东也只能选择铺天盖地的巨额广告投放,在企
项目价值、业务场景及风险、结构差异对于API对接类项目而言,是需求调研的重点,做好这几个关键性的调研,项目基本成功了一半。笔者火
细雨算法是18年6月底公开,7月中旬开始实施的算法,我现在更新这个可能有点晚了,这个小编有点懒。我们先了解下细雨算法宗旨是,提升用户
飓风算法早在17年7月份的时候就已经发布出来了,时隔一年2个月算法再次升级,飓风算法2.0出现,再次对采集类网站敲响了警钟。对于采集