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