应业务需求,实现输入一个地址,调用高德的地图的api将返回解析后的地址
高德地图的官方说明:https://lbs.amap.com/api/webservice/guide/api/georegeo
第一步,注册一个账号,创建一个应用取得appkey
第二步,仔细研读官网api,封装一个返回的实体类
- package com.handkoo.entity;
- import java.util.List;
- /**
- * 输入地址返回解析结果的类
- *
- * @author cp
- *
- */
- 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;
- }
- }
- package com.handkoo.entity;
- import javax.print.DocFlavor.STRING;
- 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;
- }
- }
第三步,调用api请求,解析返回的JSON数据封装到对象中
- package com.handkoo.util;
- import java.util.ArrayList;
- import java.util.List;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.handkoo.entity.GaodeLocation;
- import com.handkoo.entity.Geocodes;
- /**
- * 高德地图测试
- *
- * @author cp
- *
- */
- public class GaoDeMapUtil {
- private static final Logger logger = LoggerFactory.getLogger(GaoDeMapUtil.class);
- // 高德应用的地址
- private static String gaodeAppID = "1da8ea5d3653cdef862b805333f3465a";
- // 地理编码地址
- private static String map_codeurl = "http://restapi.amap.com/v3/geocode/geo?parameters";
- /*
- * static { Properties properties = new Properties(); try { gaodeAppID =
- * properties.getProperty("appkey"); } catch (Exception e) {
- * e.printStackTrace(); } }
- */
- /**
- * 输入地址返回识别后的信息
- *
- * @param address
- * @return 返回的类gaodelocation,详见类
- */
- 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 gdm = new GaoDeMapUtil();
- GaodeLocation result = gdm.getLocatoin("槐古豪庭");
- System.out.println(JSON.toJSONString(result));
- }
- }