原文詳見:https://blog.csdn.net/w3chhhhhh/article/details/64444432html
未作修改,以備後續須要,特記錄於此。
應業務需求,實現輸入一個地址,調用高德的地圖的api將返回解析後的地址。
第一步,註冊一個帳號,建立一個應用取得appkey
json
第二步,根據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