fix
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package com.cool.store.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* @author zhangchenbiao
|
||||
* @FileName: GeoMapUtil
|
||||
* @Description:
|
||||
* @date 2024-04-23 11:26
|
||||
*/
|
||||
public class GeoMapUtil {
|
||||
|
||||
private static final String AMAP_API_URL = "https://restapi.amap.com/v3/geocode/regeo";
|
||||
private static final String API_KEY = "bdf789122b56e8fd3d4a4410800382a6"; // 替换为你的高德地图API密钥
|
||||
|
||||
public static void main(String[] args) {
|
||||
AddressInfo addressInfo = reverseGeoCoding("30.41875", "120.2985");
|
||||
System.out.println(JSONObject.toJSONString(addressInfo));
|
||||
System.out.println(JSONObject.toJSONString(reverseGeoCoding("30.41", "120.29")));
|
||||
System.out.println(JSONObject.toJSONString(reverseGeoCoding("30.42", "120.30")));
|
||||
System.out.println(JSONObject.toJSONString(reverseGeoCoding("30.4", "120.2")));
|
||||
System.out.println(JSONObject.toJSONString(reverseGeoCoding("30.5", "120.3")));
|
||||
System.out.println(JSONObject.toJSONString(reverseGeoCoding("30.419", "120.299")));
|
||||
}
|
||||
|
||||
public static AddressInfo reverseGeoCoding(String latitude, String longitude) {
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
URL url = new URL(AMAP_API_URL + "?location=" + longitude + "," + latitude + "&output=json&key=" + API_KEY);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.connect();
|
||||
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
JSONObject jsonObject = JSONObject.parseObject(content.toString());
|
||||
// 提取并构建AddressInfo对象
|
||||
AddressInfo addressInfo = extractAddressInfo(jsonObject.getJSONObject("regeocode"));
|
||||
return addressInfo;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error occurred while making the request: " + e.getMessage());
|
||||
}finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static AddressInfo extractAddressInfo(JSONObject geoJson) {
|
||||
JSONObject addressComponent = geoJson.getJSONObject("addressComponent");
|
||||
// 根据实际响应结构解析省市区街道信息,此处仅为示例
|
||||
String province = (String) addressComponent.get("province");
|
||||
String city = (String) addressComponent.get("city");
|
||||
String district = (String) addressComponent.get("district");
|
||||
String township = (String) addressComponent.get("township");
|
||||
String address = geoJson.getString("formatted_address");
|
||||
return new AddressInfo(province, city, district, township, address);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class AddressInfo {
|
||||
private String province;
|
||||
private String city;
|
||||
private String district;
|
||||
private String township;
|
||||
private String address;
|
||||
|
||||
public AddressInfo(String province, String city, String district, String township, String address) {
|
||||
this.province = province;
|
||||
this.city = city;
|
||||
this.district = district;
|
||||
this.township = township;
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user