luodangjia
2024-06-11 9d2babb8bb99d55d76eab0558fd9f510652b270c
UserNTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/AmapGeocoding.java
@@ -61,6 +61,7 @@
        JSONObject jsonObject = new JSONObject(response.toString());
        if ("1".equals(jsonObject.getString("status"))) {
            JSONObject regeocode = jsonObject.getJSONObject("regeocode");
            String regeocodesss = regeocode.getString("formatted_address");
            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
            String cityName = addressComponent.getString("city"); // 这里改为了获取城市名称
            return cityName;
@@ -71,6 +72,37 @@
    public static String getAddress(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");
            String regeocodesss = regeocode.getString("formatted_address");
            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
            String cityName = addressComponent.getString("city"); // 这里改为了获取城市名称
            return regeocodesss;
        } 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; // 经度