JAVA调用百度地图API获取地理信息

百度地图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
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用百度地图API的地理编码服务实现该功能,具体步骤如下: 1. 创建百度开发者账号并申请地图API权限。 2. 在Java项目中引入百度地图APIJava SDK。 3. 调用地理编码服务的API,传入地址参数即可获取对应的经纬度信息。 以下是Java代码示例: ``` import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.utils.CoordinateConverter; import com.baidu.mapapi.utils.CoordinateConverter.CoordType; import com.baidu.mapapi.utils.HttpUtil; public class GeoCoder { private static final String GEOCODER_URL = "http://api.map.baidu.com/geocoder/v2/"; private static final String AK = "your_ak"; // 替换成申请的AK public static LatLng getLatLng(String address) { String url = GEOCODER_URL + "?address=" + address + "&output=json&ak=" + AK; String response = HttpUtil.get(url, "utf-8"); JSONObject jsonObject = JSONObject.parseObject(response); int status = jsonObject.getInteger("status"); if (status == 0) { JSONObject result = jsonObject.getJSONObject("result"); JSONObject location = result.getJSONObject("location"); double lat = location.getDouble("lat"); double lng = location.getDouble("lng"); // 将百度地图坐标转换为GPS坐标 LatLng baiduLatLng = new LatLng(lat, lng); CoordinateConverter converter = new CoordinateConverter(); converter.from(CoordType.COMMON); converter.coord(baiduLatLng); LatLng gpsLatLng = converter.convert(); return gpsLatLng; } else { return null; } } } ``` 其中,AK是百度地图API的密钥,需要替换成您自己申请的密钥。该代码实现了将百度地图坐标转换为GPS坐标,如果不需要转换可以去掉相应的代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值