原文详见:https://blog.csdn.net/w3chhhhhh/article/details/64444432html
未作修改,以备后续须要,特记录于此。
应业务需求,实现输入一个地址,调用高德的地图的api将返回解析后的地址。
第一步,注册一个帐号,建立一个应用取得appkeyjson
第二步,根据API,将其封装一个返回的实体类:api
/* 输入地址返回解析结果类 */ public class GaodeLocation { private String status;// 结果状态0,表示失败,1:表示成功 private String count;// 返回结果的数目 private String info;// 返回状态说明 private List<Geocodes> geocodes;// 识别的结果列表 public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getCount() { return count; } public void setCount(String count) { this.count = count; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public List<Geocodes> getGeocodes() { return geocodes; } public void setGeocodes(List<Geocodes> geocodes) { this.geocodes = geocodes; } public GaodeLocation(String status, String count, String info, List<Geocodes> geocodes) { super(); this.status = status; this.count = count; this.info = info; this.geocodes = geocodes; } public GaodeLocation() { super(); } @Override public String toString() { return "GaodeLocation [status=" + status + ", count=" + count + ", info=" + info + "]"; } } /* 识别结果类 */ public class Geocodes { // 结构化地址信息 private String formatted_address; // 所在省 private String province; // 城市 private String city; // 城市编码 private String citycode; // 地址所在的区 private String district; // 地址所在的乡镇 private String township; // 街道 private String street; // 门牌 private String number; // 区域编码 private String adcode; // 坐标点 private String location; // 匹配级别 private String level; public String getFormatted_address() { return formatted_address; } public void setFormatted_address(String formatted_address) { this.formatted_address = formatted_address; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCitycode() { return citycode; } public void setCitycode(String citycode) { this.citycode = citycode; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getTownship() { return township; } public void setTownship(String township) { this.township = township; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getAdcode() { return adcode; } public void setAdcode(String adcode) { this.adcode = adcode; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String getLevel() { return level; } public void setLevel(String level) { this.level = level; } public Geocodes(String formatted_address, String province, String city, String citycode, String district, String township, String street, String number, String adcode, String location, String level) { super(); this.formatted_address = formatted_address; this.province = province; this.city = city; this.citycode = citycode; this.district = district; this.township = township; this.street = street; this.number = number; this.adcode = adcode; this.location = location; this.level = level; } public Geocodes() { super(); } @Override public String toString() { return "Geocodes [formatted_address=" + formatted_address + ", province=" + province + ", city=" + city + ", citycode=" + citycode + ", district=" + district + ", township=" + township + ", street=" + street + ", number=" + number + ", adcode=" + adcode + ", location=" + location + ", level=" + level + "]"; } }
第三步,调用api请求,解析返回的JSON数据封装到对象中app
public class GaoDeMapUtil { private static final Logger logger = LoggerFactory.getLogger(GaoDeMapUtil.class); // 高德应用的地址 private static String gaodeAppID = "7ff46e1a64fade07c3cbc5eee118ad1d"; // 地理编码地址 private static String map_codeurl = "http://restapi.amap.com/v3/geocode/geo?parameters"; /** * 输入地址返回识别的信息 */ public GaodeLocation getLocatoin(String address) { GaodeLocation location = null; if (address != null) { try { location = new GaodeLocation(); String url = map_codeurl.replace("parameters", ""); String params = "key=" + gaodeAppID + "&address=" + address; logger.info("高德地图params:" + params); String result = OKHttpUtil.httpPost(url, params); logger.info("高德地图返回结果:" + result); JSONObject jsonObject = JSONObject.parseObject(result); // 解析json location.setStatus(jsonObject.getString("status")); location.setInfo(jsonObject.getString("info")); location.setCount(jsonObject.getString("count")); List<Geocodes> geocodes = new ArrayList<>(); JSONArray jsonArray = jsonObject.getJSONArray("geocodes"); // 遍历解析出来的结果 if ((jsonArray != null) && (jsonArray.size() > 0)) { JSONObject jo = (JSONObject) jsonArray.get(0); Geocodes go = new Geocodes(); go.setFormatted_address(jo.getString("formatted_address")); go.setProvince(jo.getString("province")); go.setCitycode(jo.getString("citycode")); go.setCity(jo.getString("city")); go.setDistrict(jo.getString("district")); // 地址所在的乡镇 JSONArray ts = jo.getJSONArray("township"); if (ts != null && ts.size() > 0) { go.setTownship(ts.getString(0)); } // 地址编号 go.setAdcode(jo.getString("adcode")); // 街道 JSONArray jd = jo.getJSONArray("street"); if (jd != null && jd.size() > 0) { go.setStreet(jd.getString(0)); } // 号码 JSONArray hm = jo.getJSONArray("number"); if (hm != null && hm.size() > 0) { go.setStreet(hm.getString(0)); } go.setLocation(jo.getString("location")); go.setLevel(jo.getString("level")); geocodes.add(go); } location.setGeocodes(geocodes); } catch (Exception e) { e.printStackTrace(); } } return location; } /* 测试数据 */ public static void main(String[] args) { GaoDeMapUtil gdMapUtil=new GaoDeMapUtil(); GaodeLocation result=gdMapUtil.getLocatoin(''上海海洋馆"); System.out.println(JSON.toJSONString(result)); } }
OKHttpUtil类:ide
public class OKHttpUtil { /** * 发起get请求 * * @param url * @return */ public static String httpGet(String url) { String result = null; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); try { Response response = client.newCall(request).execute(); result = response.body().string(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 发送httppost请求 * * @param url * @param data * 提交的参数为key=value&key1=value1的形式 * @return */ public static String httpPost(String url, String data) { String result = null; OkHttpClient httpClient = new OkHttpClient(); RequestBody requestBody = RequestBody.create(MediaType.parse("text/html;charset=utf-8"), data); Request request = new Request.Builder().url(url).post(requestBody).build(); try { Response response = httpClient.newCall(request).execute(); result = response.body().string(); } catch (IOException e) { e.printStackTrace(); } return result; } }
第四步,测试结果:post