逆地理编码的前端地图已经调用了,直接使用前端结果,去除地理编码工具类

This commit is contained in:
jkcdev 2025-04-22 09:49:06 +08:00
parent cf598e7122
commit cb4bb2f090
7 changed files with 17 additions and 190 deletions

View File

@ -1,22 +0,0 @@
package com.cdzy.orders.config;
import com.cdzy.orders.uitls.GeoCodingUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = "geo-coding")
public class GeoCodingConfig {
private String apiUrl;
private String accessKey;
@Bean
public GeoCodingUtil geoCodingUtil() {
return new GeoCodingUtil(apiUrl, accessKey);
}
}

View File

@ -1,5 +1,6 @@
package com.cdzy.orders.model.dto.req;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -58,4 +59,10 @@ public class EbikeUserBacksiteSaveDto implements Serializable {
*/
private String applyReason;
/**
* 行政区划名称
*/
@NotBlank(message = "行政区划名称不能为空")
private String zoneName;
}

View File

@ -99,4 +99,9 @@ public class EbikeUserBacksiteDto implements Serializable {
* 区域ID
*/
private String regionId;
/**
* 区域名称
*/
private String regionName;
}

View File

@ -48,7 +48,7 @@ public class EbikeUserBacksiteDeal implements Serializable {
/**
* 处理时间
*/
@Column(onInsertValue = "now()")
@Column(onUpdateValue = "now()")
private LocalDateTime dealTime;
/**

View File

@ -2,15 +2,12 @@ package com.cdzy.orders.service.impl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.cdzy.common.model.EbikeUserFaultreportDto;
import com.cdzy.common.model.JsonResult;
import com.cdzy.common.utils.CoordinateUtil;
import com.cdzy.orders.model.dto.req.EbikeUserBacksiteSaveDto;
import com.cdzy.orders.model.dto.req.ReqEbikeUserBacksiteDto;
import com.cdzy.orders.model.dto.res.EbikeUserBacksiteDto;
import com.cdzy.orders.model.entity.EbikeUserBacksiteDeal;
import com.cdzy.orders.service.EbikeUserBacksiteDealService;
import com.cdzy.orders.uitls.GeoCodingUtil;
import com.ebike.feign.clients.OperateFeignClient;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
@ -38,8 +35,7 @@ import static com.cdzy.orders.model.entity.table.EbikeUserBacksiteTableDef.EBIKE
@Slf4j
@Service
public class EbikeUserBacksiteServiceImpl extends ServiceImpl<EbikeUserBacksiteMapper, EbikeUserBacksite> implements EbikeUserBacksiteService{
@Resource
private GeoCodingUtil geoApiService;
@Resource
private OperateFeignClient ebikeOperateService;
@Resource
@ -49,21 +45,15 @@ public class EbikeUserBacksiteServiceImpl extends ServiceImpl<EbikeUserBacksiteM
@Override
public boolean save(EbikeUserBacksiteSaveDto ebikeUserBacksiteSaveDto) {
try{
//经纬度坐标反向获取行政区划
double[] location = CoordinateUtil.WGS84ToGCJ02(ebikeUserBacksiteSaveDto.getApplyLng(), ebikeUserBacksiteSaveDto.getApplyLat());
String regionName = geoApiService.getDistrictName(location[0], location[1]);
//根据行政区划获取运营区域生成不同的处理信息
if(regionName == null||regionName.isEmpty()){
return false;
}
JsonResult<?> jsonResult = ebikeOperateService.getRegionIdsByZone(regionName);
String zoneName = ebikeUserBacksiteSaveDto.getZoneName();
JsonResult<?> jsonResult = ebikeOperateService.getRegionIdsByZone(zoneName);
if(jsonResult.getCode()!=200){
log.error("获取运营区域失败,{}", jsonResult.getMessage());
return false;
}
List<Long> regionIds = fromRestResult(jsonResult.getData());
if(regionIds == null||regionIds.isEmpty()){
log.error("{}内没有设置运营区域", regionName);
log.error("{}内没有设置运营区域", zoneName);
return false;
}
EbikeUserBacksite ebikeUserBacksite = new EbikeUserBacksite();

View File

@ -1,150 +0,0 @@
package com.cdzy.orders.uitls;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;
import java.util.Arrays;
/**
* 地址解析反解析工具类
*
* @author dingchao
* @date 2025/4/3
* @modified by:
*/
@Slf4j
@Service
public class GeoCodingUtil {
private final static String LOCATION_TO_ADDRESS = "location";
private final static String ADDRESS_TO_LOCATION = "address";
private final String url;
private final String accessKey;
private final OkHttpClient client;
/**
* 地理编码工具类构造函数
* 目前实现的腾讯地图webservice
*
* @param apiUrl
* @param accessKey
*/
public GeoCodingUtil(String apiUrl, String accessKey) {
this.url = apiUrl;
this.accessKey = accessKey;
this.client = new OkHttpClient();
}
/**
* 输入经纬度返回地址
*
* @param location 经纬度
* @return 地址
*/
public String getLocationToaddress(JSONObject location) {
JSONObject jsonObject = getLocationToAddress(location.getDouble("lat"), location.getDouble("lng"));
if (jsonObject == null) {
return null;
}
if (jsonObject.getInteger("status") == 0) {
return jsonObject.getJSONObject("result").getJSONObject("formatted_addresses").getString("standard_address");
}
logError("地址解析失败==>{}", jsonObject.getString("message"));
return null;
}
/**
* 输入经纬度返回行政区划区县级
*
* @param longitude 经度GCJ02坐标系
* @param latitude 纬度GCJ02坐标系
* @return 行政区划区县级
*/
public String getDistrictName(Double longitude, Double latitude) {
JSONObject jsonObject = getLocationToAddress(latitude, longitude);
if (jsonObject == null) {
return null;
}
if (jsonObject.getInteger("status") == 0) {
JSONObject ad_address = jsonObject.getJSONObject("result").getJSONObject("ad_info");
if (ad_address == null) {
logError("获取行政区划失败==>{}", "ad_info为空");
return null;
}
return ad_address.getString("district");
}
logError("地址解析失败==>{}", jsonObject.getString("message"));
return null;
}
/**
* 经纬度转文字地址及相关位置信息
*
* @param latitude 纬度GCJ02坐标系
* @param longitude 经度GCJ02坐标系
* @return 返回文字地址及相关位置信息
*/
private JSONObject getLocationToAddress(Double latitude, Double longitude) {
Request request = new Request.Builder()
.url(url + "/?"+LOCATION_TO_ADDRESS+"=" + String.format("%f,%f", latitude, longitude) + "&key=" + accessKey)
.build();
try(Response response = client.newCall(request).execute()) {
if(response.isSuccessful()) {
if (response.body()!= null) {
String result = response.body().string();
return JSONObject.parseObject(result);
}
logError("地址解析失败==>{}", response.message());
return null;
}
logError("地址解析失败==>{}", response.message());
return null;
} catch (Exception e) {
logError("地址解析失败==>{}", e.getMessage() + Arrays.toString(e.getStackTrace()));
return null;
}
}
/**
* 输入地址返回经纬度(GCJ02)
*
* @param address 地址
* @return 经纬度
*/
public JSONObject getAddressToLocation(String address) {
Request request = new Request.Builder()
.url(url + "/?"+ADDRESS_TO_LOCATION+"=" + 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");
}else{
logError("位置解析失败==>{}", jsonObject.getString("message"));
return null;
}
}else{
logError("位置解析失败==>{}", response.message());
return null;
}
}else{
logError("位置解析失败==>{}", response.message());
return null;
}
} catch (Exception e) {
logError("位置解析失败==>{}", e.getMessage() + Arrays.toString(e.getStackTrace()));
return null;
}
}
private void logError(String errDesc, String errorMessage) {
log.error(errDesc, errorMessage);
}
}

View File

@ -93,6 +93,3 @@ minio:
access-key: eQtGmQBEsGxNHrTd7AkJ # 访问密钥
secret-key: Zg6X6j0kgUT1fGsGSgoCZWu6fgL8F3Kw1FfoX4yJ # 私有密钥
bucket-name: test
geo-coding:
api-url: https://apis.map.qq.com/ws/geocoder/v1
access-key: 5LUBZ-QHHWZ-BMVXQ-7NYZZ-PXG4V-YXFFI