修改完善地理编码服务,切换为高德地图地理/逆地理编码服务

This commit is contained in:
jkcdev 2025-05-23 16:44:50 +08:00
parent 5486d9ffc5
commit d88bfd6a48
2 changed files with 35 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import java.util.Arrays;
/**
* 地址解析反解析工具类
* 目前实现的高德地图webservice,位置是[经度, 维度], 腾讯地图webservice的位置是[维度, 经度]
*
* @author dingchao
* @date 2025/4/3
@ -19,8 +20,16 @@ import java.util.Arrays;
@Slf4j
@Service
public class GeoCodingUtil {
private final static String LOCATION_TO_ADDRESS = "location";
private final static String ADDRESS_TO_LOCATION = "address";
// webservice接口腾讯地图webservice 为空
private final static String LOCATION_TO_ADDRESS = "regeo";
private final static String ADDRESS_TO_LOCATION = "geo";
private final static String STATUS_KEY = "status";
// 结果键 高德地图webservice 为geocodesregeocode腾讯地图webservice为result
private final static String ADDRESS_RESULT_KEY = "regeocode";
private final static String LOCATION_RESULT_KEY = "geocodes";
//private final static String RESULT_KEY = "geocodes";
// 成功状态码 高德地图webservice 为1腾讯地图webservice为0
private final static int CODE_STATUS_SUCCESS = 1;
private final String url;
private final String accessKey;
@ -47,15 +56,16 @@ public class GeoCodingUtil {
*/
public String getLocationToAddress(JSONObject location) {
Request request = new Request.Builder()
.url(url + "/?"+LOCATION_TO_ADDRESS+"=" + String.format("%f,%f", location.getDouble("lat"), location.getDouble("lng")) + "&key=" + accessKey)
.url(url + "/"+LOCATION_TO_ADDRESS + "?"+"location=" + String.format("%f,%f", location.getDouble("lng"), location.getDouble("lat")) + "&key=" + accessKey)
.build();
try(Response response = client.newCall(request).execute()) {
if(response.isSuccessful()) {
if (response.body()!= null) {
String result = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.getInteger("status") == 0) {
return jsonObject.getJSONObject("result").getJSONObject("formatted_addresses").getString("standard_address");
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
//return jsonObject.getJSONObject(RESULT_KEY).getJSONObject("formatted_addresses").getString("standard_address");
return jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getString("formatted_address");
}
logError("地址解析失败==>{}", jsonObject.getString("message"));
return null;
@ -79,20 +89,23 @@ public class GeoCodingUtil {
*/
public JSONObject getLocationToAddressDetails(JSONObject location) {
Request request = new Request.Builder()
.url(url + "/?"+LOCATION_TO_ADDRESS+"=" + String.format("%f,%f", location.getDouble("lat"), location.getDouble("lng")) + "&key=" + accessKey)
.url(url + "/"+LOCATION_TO_ADDRESS + "?location=" + String.format("%f,%f", location.getDouble("lng"), location.getDouble("lat")) + "&key=" + accessKey)
.build();
try(Response response = client.newCall(request).execute()) {
if(response.isSuccessful()) {
if (response.body()!= null) {
String result = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.getInteger("status") == 0) {
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
JSONObject address = new JSONObject();
String detail = jsonObject.getJSONObject("result").getJSONObject("formatted_addresses").getString("standard_address");
//String detail = jsonObject.getJSONObject("result").getJSONObject("formatted_addresses").getString("standard_address");
String detail = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getString("formatted_address");
address.put("detail", detail);
String district = jsonObject.getJSONObject("result").getJSONObject("ad_info").getString("district");
//String district = jsonObject.getJSONObject("result").getJSONObject("ad_info").getString("district");
String district = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject("addressComponent").getString("district");
address.put("district", district);
String adcode = jsonObject.getJSONObject("result").getJSONObject("ad_info").getString("adcode");
//String adcode = jsonObject.getJSONObject("result").getJSONObject("ad_info").getString("adcode");
String adcode = jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getJSONObject("addressComponent").getString("adcode");
address.put("adcode", adcode);
return address;
}
@ -118,15 +131,21 @@ public class GeoCodingUtil {
*/
public JSONObject getAddressToLocation(String address) {
Request request = new Request.Builder()
.url(url + "/?"+ADDRESS_TO_LOCATION+"=" + address + "&key=" + accessKey)
.url(url + "/"+ADDRESS_TO_LOCATION + "?address=" + address + "&key=" + accessKey)
.build();
try(Response response = client.newCall(request).execute()) {
if(response.isSuccessful()) {
if (response.body() != null) {
String result = response.body().string();
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject.getInteger("status") == 0) {
return jsonObject.getJSONObject("result").getJSONObject("location");
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
//return jsonObject.getJSONObject(LOCATION_RESULT_KEY).getJSONObject("location");
String loc = jsonObject.getJSONArray(LOCATION_RESULT_KEY).getJSONObject(0).getString("location");
String[] locArr = loc.split(",");
JSONObject location = new JSONObject();
location.put("lng", Double.valueOf(locArr[0]));
location.put("lat", Double.valueOf(locArr[1]));
return location;
}else{
logError("位置解析失败==>{}", jsonObject.getString("message"));
return null;

View File

@ -3,7 +3,7 @@ spring:
name: ebike-maintenance
cloud:
nacos:
server-addr: 192.168.2.226:8848 # nacos
server-addr: 127.0.0.1:8848 # nacos
username: nacos
password: nacos
jackson:
@ -84,5 +84,5 @@ minio:
secret-key: Zg6X6j0kgUT1fGsGSgoCZWu6fgL8F3Kw1FfoX4yJ # 私有密钥
bucket-name: test
geo-coding:
api-url: https://apis.map.qq.com/ws/geocoder/v1
access-key: BECBZ-EJIEQ-LUU5N-B5ISQ-3TLMZ-BXFLG
api-url: https://restapi.amap.com/v3/geocode
access-key: 14c06210d417b30fb8a66d27b0b31a62