百度地图API之JAVA
JAVA调用百度地图API获取地理信息工具
注意: 没有认证的(服务器端)AK每天只能使用6000次左右
-- 百度控制台申请AK,注册登录->应用管理->我的应用->创建应用
http://lbsyun.baidu.com/apiconsole/center#/home
-- 错误码对照表
http://lbs.baidu.com/index.php?title=iossdk/guide/addition-func/errorcode
-- 更多请查看地图调起API
http://lbsyun.baidu.com/index.php?title=uri/api/web
1.获取指定行政区的中心经纬度 [getLngAndLatByAddress]
@SpringBootTest
@RunWith(SpringRunner.class)
public class BaiduMap {
final static String AK = "登陆百度地图控制台申请服务端AK";
final static String ADDRESS_TO_LONGITUDEA_URL = "http://api.map.baidu.com/geocoding/v3/?output=json";
final static String ADDRESS = "河南省洛阳市";
@Test
public void getLngAndLatByAddress() {
if (!StringUtils.isBlank(ADDRESS)) {
String url = ADDRESS_TO_LONGITUDEA_URL + "&ak=" + AK + "&address=" + ADDRESS;
HttpClient client = HttpClients.createDefault(); // 创建默认http连接
HttpPost post = new HttpPost(url);// 创建一个post请求
try {
HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
HttpEntity entity = response.getEntity();// 从response中取到响实体
String json = EntityUtils.toString(entity);// 把响应实体转成文本
// JSON转对象
if (json != null && !"".equals(json)) {
JSONObject obj = JSONObject.fromObject(json);
if ("0".equals(obj.getString("status"))) {
// status 状态码 0-成功
}
}
} catch (Exception e) {
}
}
}
}
响应:
{
"status": 0,
"result": {
"location": {
"lng": 112.4594212983115,
"lat": 34.62426277921943
},
"precise": 0,
"confidence": 20,
"comprehension": 100,
"level": "城市"
}
}
2.获取指定经纬度的行政区信息 [getRegionByLngAndLat]
@SpringBootTest
@RunWith(SpringRunner.class)
public class BaiduMap {
final static String AK = "登陆百度地图控制台申请服务端AK";
final static String LOCATION_TO_LONGITUDEA_URL = "http://api.map.baidu.com/reverse_geocoding/v3/?output=json";
final static String LNG = "112.459420";
final static String LAT = "34.624264";
@Test
public void getRegionByLngAndLat() {
String url = LOCATION_TO_LONGITUDEA_URL+"&ak="+AK+"&location="+LAT+","+LNG;
HttpClient client = HttpClients.createDefault(); // 创建默认http连接
HttpPost post = new HttpPost(url);// 创建一个post请求
try {
HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
HttpEntity entity = response.getEntity();// 从response中取到响实体
String json = EntityUtils.toString(entity);// 把响应实体转成文本
// JSON转对象
if (json != null && !"".equals(json)) {
JSONObject obj = JSONObject.fromObject(json);
if ("0".equals(obj.getString("status"))) {
String regionCode = obj.getJSONObject("result").getJSONObject("addressComponent").getString("adcode"); // 区域编码
}else if("302".equals(obj.getString("status"))){
// 302-使用次数超限,认证或第二天继续使用或换一个AK
System.out.println(json);
}
}
} catch (Exception e) {
}
}
}
响应
{
"status": 0,
"result": {
"location": {
"lng": 112.45941999999994,
"lat": 34.624263987092529
},
"formatted_address": "河南省洛阳市洛龙区开元大道230号",
"business": "泉舜广场,宝龙广场,关林",
"addressComponent": {
"country": "中国",
"country_code": 0,
"country_code_iso": "CHN",
"country_code_iso2": "CN",
"province": "河南省",
"city": "洛阳市",
"city_level": 2,
"district": "洛龙区",
"town": "",
"town_code": "",
"adcode": "410311",//洛龙区行政区域code
"street": "开元大道",
"street_number": "230号",
"direction": "附近",
"distance": "27"
},
"pois": [],
"roads": [],
"poiRegions": [],
"sematic_description": "",
"cityCode": 153
}
}