修改完善地理编码服务,切换为高德地图地理/逆地理编码服务
This commit is contained in:
parent
5486d9ffc5
commit
d88bfd6a48
@ -11,6 +11,7 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 地址解析、反解析工具类。
|
* 地址解析、反解析工具类。
|
||||||
|
* 目前实现的高德地图webservice,位置是[经度, 维度], 腾讯地图webservice的位置是[维度, 经度]
|
||||||
*
|
*
|
||||||
* @author dingchao
|
* @author dingchao
|
||||||
* @date 2025/4/3
|
* @date 2025/4/3
|
||||||
@ -19,8 +20,16 @@ import java.util.Arrays;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class GeoCodingUtil {
|
public class GeoCodingUtil {
|
||||||
private final static String LOCATION_TO_ADDRESS = "location";
|
// webservice接口;腾讯地图webservice 为空
|
||||||
private final static String ADDRESS_TO_LOCATION = "address";
|
private final static String LOCATION_TO_ADDRESS = "regeo";
|
||||||
|
private final static String ADDRESS_TO_LOCATION = "geo";
|
||||||
|
private final static String STATUS_KEY = "status";
|
||||||
|
// 结果键 高德地图webservice 为geocodes、regeocode,腾讯地图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 url;
|
||||||
private final String accessKey;
|
private final String accessKey;
|
||||||
@ -47,15 +56,16 @@ public class GeoCodingUtil {
|
|||||||
*/
|
*/
|
||||||
public String getLocationToAddress(JSONObject location) {
|
public String getLocationToAddress(JSONObject location) {
|
||||||
Request request = new Request.Builder()
|
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();
|
.build();
|
||||||
try(Response response = client.newCall(request).execute()) {
|
try(Response response = client.newCall(request).execute()) {
|
||||||
if(response.isSuccessful()) {
|
if(response.isSuccessful()) {
|
||||||
if (response.body()!= null) {
|
if (response.body()!= null) {
|
||||||
String result = response.body().string();
|
String result = response.body().string();
|
||||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||||
if (jsonObject.getInteger("status") == 0) {
|
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
|
||||||
return jsonObject.getJSONObject("result").getJSONObject("formatted_addresses").getString("standard_address");
|
//return jsonObject.getJSONObject(RESULT_KEY).getJSONObject("formatted_addresses").getString("standard_address");
|
||||||
|
return jsonObject.getJSONObject(ADDRESS_RESULT_KEY).getString("formatted_address");
|
||||||
}
|
}
|
||||||
logError("地址解析失败==>{}", jsonObject.getString("message"));
|
logError("地址解析失败==>{}", jsonObject.getString("message"));
|
||||||
return null;
|
return null;
|
||||||
@ -79,20 +89,23 @@ public class GeoCodingUtil {
|
|||||||
*/
|
*/
|
||||||
public JSONObject getLocationToAddressDetails(JSONObject location) {
|
public JSONObject getLocationToAddressDetails(JSONObject location) {
|
||||||
Request request = new Request.Builder()
|
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();
|
.build();
|
||||||
try(Response response = client.newCall(request).execute()) {
|
try(Response response = client.newCall(request).execute()) {
|
||||||
if(response.isSuccessful()) {
|
if(response.isSuccessful()) {
|
||||||
if (response.body()!= null) {
|
if (response.body()!= null) {
|
||||||
String result = response.body().string();
|
String result = response.body().string();
|
||||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||||
if (jsonObject.getInteger("status") == 0) {
|
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
|
||||||
JSONObject address = new JSONObject();
|
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);
|
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);
|
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);
|
address.put("adcode", adcode);
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
@ -118,15 +131,21 @@ public class GeoCodingUtil {
|
|||||||
*/
|
*/
|
||||||
public JSONObject getAddressToLocation(String address) {
|
public JSONObject getAddressToLocation(String address) {
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(url + "/?"+ADDRESS_TO_LOCATION+"=" + address + "&key=" + accessKey)
|
.url(url + "/"+ADDRESS_TO_LOCATION + "?address=" + address + "&key=" + accessKey)
|
||||||
.build();
|
.build();
|
||||||
try(Response response = client.newCall(request).execute()) {
|
try(Response response = client.newCall(request).execute()) {
|
||||||
if(response.isSuccessful()) {
|
if(response.isSuccessful()) {
|
||||||
if (response.body() != null) {
|
if (response.body() != null) {
|
||||||
String result = response.body().string();
|
String result = response.body().string();
|
||||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||||
if (jsonObject.getInteger("status") == 0) {
|
if (jsonObject.getInteger(STATUS_KEY) == CODE_STATUS_SUCCESS) {
|
||||||
return jsonObject.getJSONObject("result").getJSONObject("location");
|
//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{
|
}else{
|
||||||
logError("位置解析失败==>{}", jsonObject.getString("message"));
|
logError("位置解析失败==>{}", jsonObject.getString("message"));
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@ -3,7 +3,7 @@ spring:
|
|||||||
name: ebike-maintenance
|
name: ebike-maintenance
|
||||||
cloud:
|
cloud:
|
||||||
nacos:
|
nacos:
|
||||||
server-addr: 192.168.2.226:8848 # nacos
|
server-addr: 127.0.0.1:8848 # nacos
|
||||||
username: nacos
|
username: nacos
|
||||||
password: nacos
|
password: nacos
|
||||||
jackson:
|
jackson:
|
||||||
@ -84,5 +84,5 @@ minio:
|
|||||||
secret-key: Zg6X6j0kgUT1fGsGSgoCZWu6fgL8F3Kw1FfoX4yJ # 私有密钥
|
secret-key: Zg6X6j0kgUT1fGsGSgoCZWu6fgL8F3Kw1FfoX4yJ # 私有密钥
|
||||||
bucket-name: test
|
bucket-name: test
|
||||||
geo-coding:
|
geo-coding:
|
||||||
api-url: https://apis.map.qq.com/ws/geocoder/v1
|
api-url: https://restapi.amap.com/v3/geocode
|
||||||
access-key: BECBZ-EJIEQ-LUU5N-B5ISQ-3TLMZ-BXFLG
|
access-key: 14c06210d417b30fb8a66d27b0b31a62
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user