申请ak(即获取密钥)
http://lbsyun.baidu.com/apiconsole/key?application=key 去这儿注册一个开发者账号即可
拼写发送http请求的url
譬如这样的调用
http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=E4805d16520de693a3fe707cdc962045&callback=showLocation
接收http请求返回的数据
下面看看代码
/** * 接口常量 * @author rex * */ public interface BaiDuApi { /* * 根据ip获取信息 */ public static final String BD_IP_LOCATION_API = "http://api.map.baidu.com/location/ip"; /* *根据城市/经纬度获取信息 */ public static final String BD_LOCATION2POINT_API = "http://api.map.baidu.com/geocoder/v2/"; /* * 百度lbs ak密钥 */ public static final String BD_LBS_AK = "你申请的AK"; }
/* * 百度地图API */ public final class BaiDuMapApi { private static Map<String, String> params = CollectionUtil.newHashMap(); private static final List<String> LOCAL_IP = Arrays.asList("127.0.0.1", "localhost"); /* * 根据ip获取经纬度 */ public static Map<String, String> getPoint(String ip) { params.clear(); Map<String, String> point = CollectionUtil.newHashMap(); if (null != ip && !LOCAL_IP.contains(ip)) { params.put("ip", ip); } params.put("ak", BaiDuApi.BD_LBS_AK); params.put("coor", "bd09ll"); String r = HttpKit.get(BaiDuApi.BD_IP_LOCATION_API, params); String json = StringUtils.unicodeToString(r); Map<String, Object> map = JSONUtil.json2Map(json); Map<String, Object> content = JSONUtil.json2Map(map.get("content").toString()); Map<String, String> xy = JSONUtil.json2Map(content.get("point").toString()); point.put("latitude", xy.get("y")); point.put("longitude", xy.get("x")); return point; } /* * 根据城市和地址获取经纬度 */ public static Map<String, String> getPoint(String city, String address) { params.clear(); Map<String, String> point = CollectionUtil.newHashMap(); if (StringUtils.isNotBlank(city) && StringUtils.isNotBlank(address)) { params.put("ak", BaiDuApi.BD_LBS_AK); params.put("callback", "renderOption"); params.put("output", "json"); params.put("city", city); params.put("address", address); String r = HttpKit.get(BaiDuApi.BD_LOCATION2POINT_API, params); String json = StringUtils.unicodeToString(r.substring(r.indexOf("{"), r.lastIndexOf("}") + 1)); Map<String, Object> map = JSONUtil.json2Map(json); Map<String, Object> content = JSONUtil.json2Map(map.get("result").toString()); Map<String, Object> location = JSONUtil.json2Map(content.get("location").toString()); point.put("latitude", location.get("lat").toString()); point.put("longitude", location.get("lng").toString()); } return point; } /* * 根据ip获取address */ public static Map<String, String> getAddress(String ip) { params.clear(); Map<String, String> address_detail = CollectionUtil.newHashMap(); if (null != ip && !LOCAL_IP.contains(ip)) { params.put("ip", ip); } params.put("ak", BaiDuApi.BD_LBS_AK); String r = HttpKit.get(BaiDuApi.BD_IP_LOCATION_API, params); String json = StringUtils.unicodeToString(r); Map<String, Object> map = JSONUtil.json2Map(json); Map<String, Object> content = JSONUtil.json2Map(map.get("content").toString()); address_detail = JSONUtil.json2Map(content.get("address_detail").toString()); return address_detail; } /* * 根据经纬度获取详细地址 */ public static Map<String, Object> getAddress(String latitude, String longitude) { params.clear(); Map<String, Object> info = CollectionUtil.newHashMap(); if (StringUtils.isNotBlank(latitude) && StringUtils.isNotBlank(longitude)) { params.put("callback", "renderReverse"); params.put("ak", BaiDuApi.BD_LBS_AK); params.put("location", latitude + "," + longitude); params.put("output", "json"); params.put("pois", "0"); String r = HttpKit.get(BaiDuApi.BD_LOCATION2POINT_API, params); String json = StringUtils.unicodeToString(r.substring(r.indexOf("{"), r.lastIndexOf("}") + 1)); Map<String, Object> map = JSONUtil.json2Map(json); info = JSONUtil.json2Map(map.get("result").toString()); } return info; } }
/* * 百度地图API测试 */ public class Test { public static void main(String[] args) { //获取地址信息 Map<String, String> m1 = BaiDuMapApi.getAddress(null); System.out.println(m1); //获取经纬度信息 Map<String, String> m2 = BaiDuMapApi.getPoint(null); System.out.println(m2); //根据城市获取经纬度 Map<String, String> m3 = BaiDuMapApi.getPoint("上海市", "东方明珠"); System.out.println(m3); //根据经纬度获取地址 Map<String, Object> m4 = BaiDuMapApi.getAddress(m3.get("latitude"), m3.get("longitude")); System.out.println(m4); } }
{province=上海市, city=上海市, street=, district=, street_number=, city_code=289} {latitude=31.24916171, longitude=121.48789949} {latitude=31.244750205504, longitude=121.50713723717} {formatted_address=上海市浦东新区陆家嘴环路1388号, business=东外滩,陆家嘴,外滩, cityCode=289, location={"lat":31.244750051136,"lng":121.50713723717}, addressComponent={"city":"上海市","district":"浦东新区","province":"上海市","street":"陆家嘴环路","street_number":"1388号"}}