调用高德地图api

应业务需求,实现输入一个地址,调用高德的地图的api将返回解析后的地址

高德地图的官方说明:https://lbs.amap.com/api/webservice/guide/api/georegeo


第一步,注册一个账号,创建一个应用取得appkey


第二步,仔细研读官网api,封装一个返回的实体类

[java]  view plain  copy
  1. package com.handkoo.entity;  
  2.   
  3. import java.util.List;  
  4.   
  5. /** 
  6.  * 输入地址返回解析结果的类 
  7.  *  
  8.  * @author cp 
  9.  * 
  10.  */  
  11. public class GaodeLocation {  
  12.     private String status;// 结果状态0,表示失败,1:表示成功  
  13.     private String count;// 返回结果的数目  
  14.     private String info;// 返回状态说明  
  15.     private List<Geocodes> geocodes;// 识别的结果列表  
  16.   
  17.     public String getStatus() {  
  18.         return status;  
  19.     }  
  20.   
  21.     public void setStatus(String status) {  
  22.         this.status = status;  
  23.     }  
  24.   
  25.     public String getCount() {  
  26.         return count;  
  27.     }  
  28.   
  29.     public void setCount(String count) {  
  30.         this.count = count;  
  31.     }  
  32.   
  33.     public String getInfo() {  
  34.         return info;  
  35.     }  
  36.   
  37.     public void setInfo(String info) {  
  38.         this.info = info;  
  39.     }  
  40.   
  41.     public List<Geocodes> getGeocodes() {  
  42.         return geocodes;  
  43.     }  
  44.   
  45.     public void setGeocodes(List<Geocodes> geocodes) {  
  46.         this.geocodes = geocodes;  
  47.     }  
  48.   
  49. }  

[java]  view plain  copy
  1. package com.handkoo.entity;  
  2.   
  3. import javax.print.DocFlavor.STRING;  
  4.   
  5. public class Geocodes {  
  6.     // 结构化地址信息  
  7.     private String formatted_address;  
  8.     // 所在省  
  9.     private String province;  
  10.     // 城市  
  11.     private String city;  
  12.     // 城市编码  
  13.     private String citycode;  
  14.     // 地址所在的区  
  15.     private String district;  
  16.     // 地址所在的乡镇  
  17.     private String township;  
  18.     // 街道  
  19.     private String street;  
  20.     // 门牌  
  21.     private String number;  
  22.     // 区域编码  
  23.     private String adcode;  
  24.     // 坐标点  
  25.     private String location;  
  26.     // 匹配级别  
  27.     private String level;  
  28.   
  29.     public String getFormatted_address() {  
  30.         return formatted_address;  
  31.     }  
  32.   
  33.     public void setFormatted_address(String formatted_address) {  
  34.         this.formatted_address = formatted_address;  
  35.     }  
  36.   
  37.     public String getProvince() {  
  38.         return province;  
  39.     }  
  40.   
  41.     public void setProvince(String province) {  
  42.         this.province = province;  
  43.     }  
  44.   
  45.     public String getCity() {  
  46.         return city;  
  47.     }  
  48.   
  49.     public void setCity(String city) {  
  50.         this.city = city;  
  51.     }  
  52.   
  53.     public String getCitycode() {  
  54.         return citycode;  
  55.     }  
  56.   
  57.     public void setCitycode(String citycode) {  
  58.         this.citycode = citycode;  
  59.     }  
  60.   
  61.     public String getDistrict() {  
  62.         return district;  
  63.     }  
  64.   
  65.     public void setDistrict(String district) {  
  66.         this.district = district;  
  67.     }  
  68.   
  69.     public String getTownship() {  
  70.         return township;  
  71.     }  
  72.   
  73.     public void setTownship(String township) {  
  74.         this.township = township;  
  75.     }  
  76.   
  77.     public String getStreet() {  
  78.         return street;  
  79.     }  
  80.   
  81.     public void setStreet(String street) {  
  82.         this.street = street;  
  83.     }  
  84.   
  85.     public String getNumber() {  
  86.         return number;  
  87.     }  
  88.   
  89.     public void setNumber(String number) {  
  90.         this.number = number;  
  91.     }  
  92.   
  93.     public String getAdcode() {  
  94.         return adcode;  
  95.     }  
  96.   
  97.     public void setAdcode(String adcode) {  
  98.         this.adcode = adcode;  
  99.     }  
  100.   
  101.     public String getLocation() {  
  102.         return location;  
  103.     }  
  104.   
  105.     public void setLocation(String location) {  
  106.         this.location = location;  
  107.     }  
  108.   
  109.     public String getLevel() {  
  110.         return level;  
  111.     }  
  112.   
  113.     public void setLevel(String level) {  
  114.         this.level = level;  
  115.     }  
  116.   
  117. }  


第三步,调用api请求,解析返回的JSON数据封装到对象中

[java]  view plain  copy
  1. package com.handkoo.util;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8.   
  9. import com.alibaba.fastjson.JSON;  
  10. import com.alibaba.fastjson.JSONArray;  
  11. import com.alibaba.fastjson.JSONObject;  
  12. import com.handkoo.entity.GaodeLocation;  
  13. import com.handkoo.entity.Geocodes;  
  14.   
  15. /** 
  16.  * 高德地图测试 
  17.  *  
  18.  * @author cp 
  19.  * 
  20.  */  
  21. public class GaoDeMapUtil {  
  22.     private static final Logger logger = LoggerFactory.getLogger(GaoDeMapUtil.class);  
  23.   
  24.     // 高德应用的地址  
  25.     private static String gaodeAppID = "1da8ea5d3653cdef862b805333f3465a";  
  26.   
  27.     // 地理编码地址  
  28.     private static String map_codeurl = "http://restapi.amap.com/v3/geocode/geo?parameters";  
  29.   
  30.     /* 
  31.      * static { Properties properties = new Properties(); try { gaodeAppID = 
  32.      * properties.getProperty("appkey"); } catch (Exception e) { 
  33.      * e.printStackTrace(); } } 
  34.      */  
  35.   
  36.     /** 
  37.      * 输入地址返回识别后的信息 
  38.      *  
  39.      * @param address 
  40.      * @return 返回的类gaodelocation,详见类 
  41.      */  
  42.     public GaodeLocation getLocatoin(String address) {  
  43.         GaodeLocation location = null;  
  44.         if (address != null) {  
  45.             try {  
  46.                 location = new GaodeLocation();  
  47.                 String url = map_codeurl.replace("parameters""");  
  48.                 String params = "key=" + gaodeAppID + "&address=" + address;  
  49.                 logger.info("高德地图params:" + params);  
  50.                 String result = OKHttpUtil.httpPost(url, params);  
  51.   
  52.                 logger.info("高德地图返回结果:" + result);  
  53.                 JSONObject jsonObject = JSONObject.parseObject(result);  
  54.   
  55.                 // 解析json  
  56.                 location.setStatus(jsonObject.getString("status"));  
  57.                 location.setInfo(jsonObject.getString("info"));  
  58.                 location.setCount(jsonObject.getString("count"));  
  59.                 List<Geocodes> geocodes = new ArrayList<>();  
  60.                 JSONArray jsonArray = jsonObject.getJSONArray("geocodes");  
  61.                 // 遍历解析出来的结果  
  62.                 if ((jsonArray != null) && (jsonArray.size() > 0)) {  
  63.                     JSONObject jo = (JSONObject) jsonArray.get(0);  
  64.                     Geocodes go = new Geocodes();  
  65.                     go.setFormatted_address(jo.getString("formatted_address"));  
  66.                     go.setProvince(jo.getString("province"));  
  67.                     go.setCitycode(jo.getString("citycode"));  
  68.                     go.setCity(jo.getString("city"));  
  69.                     go.setDistrict(jo.getString("district"));  
  70.                     // 地址所在的乡镇  
  71.                     JSONArray ts = jo.getJSONArray("township");  
  72.                     if (ts != null && ts.size() > 0) {  
  73.                         go.setTownship(ts.getString(0));  
  74.                     }  
  75.                     // 地址编号  
  76.                     go.setAdcode(jo.getString("adcode"));  
  77.                     // 街道  
  78.                     JSONArray jd = jo.getJSONArray("street");  
  79.                     if (jd != null && jd.size() > 0) {  
  80.                         go.setStreet(jd.getString(0));  
  81.                     }  
  82.                     // 号码  
  83.                     JSONArray hm = jo.getJSONArray("number");  
  84.                     if (hm != null && hm.size() > 0) {  
  85.                         go.setStreet(hm.getString(0));  
  86.                     }  
  87.                     go.setLocation(jo.getString("location"));  
  88.                     go.setLevel(jo.getString("level"));  
  89.                     geocodes.add(go);  
  90.                 }  
  91.                 location.setGeocodes(geocodes);  
  92.             } catch (Exception e) {  
  93.                 e.printStackTrace();  
  94.             }  
  95.         }  
  96.         return location;  
  97.     }  
  98.   
  99.     public static void main(String[] args) {  
  100.         GaoDeMapUtil gdm = new GaoDeMapUtil();  
  101.         GaodeLocation result = gdm.getLocatoin("槐古豪庭");  
  102.         System.out.println(JSON.toJSONString(result));  
  103.     }  
  104. }  

第四步,测试结果:


猜你喜欢

转载自blog.csdn.net/weixin_40213204/article/details/80098645