luodangjia
2024-05-30 a3990d3644885c51f4ea0fc7a27c1097f4808bfb
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.stylefeng.guns.modular.system.util;
 
import org.codehaus.jettison.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
 
public class AmapGeocoding {
 
    private static final String AMAP_GEOCODING_API = "https://restapi.amap.com/v3/geocode/regeo";
    private static final String AMAP_KEY = "116e73b6d14f7da0292daa6037955749"; // 替换为你的高德地图API Key
 
    public static String getCityCode(double latitude, double longitude) throws Exception {
        String url = AMAP_GEOCODING_API + "?location=" + longitude + "," + latitude
                + "&output=json&key=" + AMAP_KEY + "&radius=1000&extensions=all";
 
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
 
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        connection.disconnect();
 
        JSONObject jsonObject = new JSONObject(response.toString());
        if ("1".equals(jsonObject.getString("status"))) {
            JSONObject regeocode = jsonObject.getJSONObject("regeocode");
            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
            String cityCode = addressComponent.getString("adcode"); // adcode即为citycode
            return cityCode;
        } else {
            throw new RuntimeException("Failed to fetch city code. Error message: " + jsonObject.getString("info"));
        }
    }
 
    public static String getCityName(double latitude, double longitude) throws Exception {
        String url = AMAP_GEOCODING_API + "?location=" + longitude + "," + latitude
                + "&output=json&key=" + AMAP_KEY + "&radius=1000&extensions=all";
 
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
 
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        connection.disconnect();
 
        JSONObject jsonObject = new JSONObject(response.toString());
        if ("1".equals(jsonObject.getString("status"))) {
            JSONObject regeocode = jsonObject.getJSONObject("regeocode");
            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
            String cityName = addressComponent.getString("city"); // 这里改为了获取城市名称
            return cityName;
        } else {
            throw new RuntimeException("Failed to fetch city name. Error message: " + jsonObject.getString("info"));
        }
    }
 
 
 
    public static void main(String[] args) throws Exception {
        double lat = 116.3913; // 纬度
        double lng = 39.90539; // 经度
        System.out.println("City Code: " + getCityCode(lat, lng));
    }
}