| | |
| | | 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; |
| | |
| | | |
| | | |
| | | |
| | | 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; // 经度 |