chenjiahe
2022-04-25 fb2c9fa355cc2e09bc051677dba89f86e9c0bd00
提交 | 用户 | age
8bc3c9 1 package com.hx.util.gaode;
C 2
3 import com.hx.util.HttpUtil;
4 import com.hx.util.StringUtils;
5 import net.sf.json.JSONArray;
6 import net.sf.json.JSONObject;
7
8 import java.io.UnsupportedEncodingException;
9 import java.net.URLEncoder;
10
11 /**
12  * 高德地图工具
13  * @author mgchen
14  *
15  */
16 public class GaoDeMapUtil {
17
18     /**获取经纬度*/
19     public static String URL_CODE = "https://restapi.amap.com/v3/geocode/geo";
20
21     /**
22      * 获取地址的经纬度
23      * @param address
24      * @param key
25      * @return
26      */
27     public static AddressCode addressCode(String address,String key) {
28         AddressCode addressCode = null;
29         //URL_CODE = URL_CODE+"?key="+key+"&address="+URLEncoder.encode(address,"UTF-8");//地址编译
30         URL_CODE = URL_CODE+"?key="+key+"&address="+address;
31         JSONObject data = HttpUtil.HttpURLUtil(URL_CODE,null);
32         if(data.optInt("status",0) == 1){
33             JSONArray geocodes = data.getJSONArray("geocodes");
34             if(geocodes.size()==1){
35                 JSONObject locationObj = geocodes.getJSONObject(0);
36                 String location = locationObj.optString("location");
37                 if(StringUtils.noNull(location)){
38                     String[] locations = location.split(",");
39                     if(locations.length != 2){
40                         return null;
41                     }
42                     addressCode = new AddressCode();
43                     addressCode.setLng(locations[0]);
44                     addressCode.setLat(locations[1]);
45                     addressCode.setCityCode(locationObj.optString("citycode"));
46                 }
47             }
48         }
49         return addressCode;
50     }
51
52
53 }