代码回退
This commit is contained in:
@@ -24,25 +24,14 @@ public class FadAppLocationController {
|
|||||||
private final IFadAppLocationService locationService;
|
private final IFadAppLocationService locationService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 兼容老接口:返回单一城市/区县字符串。
|
* 根据经纬度获取地址(精确到区/县)。
|
||||||
* 用 Map 包一层避开 R.ok(String) 把字符串塞到 msg 的重载冲突。
|
* <p>用 Map 包一层避开 R.ok(String) 把字符串塞到 msg 字段的重载冲突。
|
||||||
*/
|
*/
|
||||||
@GetMapping("/city")
|
@GetMapping("/city")
|
||||||
public R<Map<String, Object>> getCity(@RequestParam Double latitude,
|
public R<Map<String, Object>> getCity(@RequestParam Double latitude,
|
||||||
@RequestParam Double longitude) {
|
@RequestParam Double longitude) {
|
||||||
String city = locationService.getCityByLocation(latitude, longitude);
|
|
||||||
Map<String, Object> data = new HashMap<>();
|
Map<String, Object> data = new HashMap<>();
|
||||||
data.put("city", city);
|
data.put("city", locationService.getCityByLocation(latitude, longitude));
|
||||||
return R.ok(data);
|
return R.ok(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 精确到区县的结构化地址。
|
|
||||||
* 返回:province / city / district / township / adcode / address。
|
|
||||||
*/
|
|
||||||
@GetMapping("/detail")
|
|
||||||
public R<Map<String, Object>> getDetail(@RequestParam Double latitude,
|
|
||||||
@RequestParam Double longitude) {
|
|
||||||
return R.ok(locationService.getDetailedLocation(latitude, longitude));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
package com.ruoyi.fadapp.service;
|
package com.ruoyi.fadapp.service;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FAD APP 定位服务接口
|
* FAD APP 定位服务接口
|
||||||
*/
|
*/
|
||||||
public interface IFadAppLocationService {
|
public interface IFadAppLocationService {
|
||||||
|
|
||||||
/** 老接口:返回单一最精准的可读地址(兼容用),优先 district。 */
|
|
||||||
String getCityByLocation(Double latitude, Double longitude);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 结构化逆地理编码。
|
* 根据经纬度返回最精准的地址(精确到区/县)。
|
||||||
* 返回字段:province / city / district / township / adcode / address。
|
* 例如 "烟台市芝罘区"、"北京市朝阳区",直辖市可能只有 "朝阳区"。
|
||||||
|
*
|
||||||
|
* @param latitude 纬度
|
||||||
|
* @param longitude 经度
|
||||||
|
* @return 地址字符串,失败返回 ""
|
||||||
*/
|
*/
|
||||||
Map<String, Object> getDetailedLocation(Double latitude, Double longitude);
|
String getCityByLocation(Double latitude, Double longitude);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -28,32 +27,15 @@ public class FadAppLocationServiceImpl implements IFadAppLocationService {
|
|||||||
private String amapKey;
|
private String amapKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 兼容老接口:只取一个尽量精准的可读地址字符串。
|
* 根据经纬度返回最精准的可读地址:优先到区/县。
|
||||||
* 优先级:district → city → province。
|
* 返回示例:"烟台市芝罘区" / "北京市朝阳区" / "山东省"(兜底)。
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getCityByLocation(Double latitude, Double longitude) {
|
public String getCityByLocation(Double latitude, Double longitude) {
|
||||||
Map<String, Object> d = getDetailedLocation(latitude, longitude);
|
if (latitude == null || longitude == null) return "";
|
||||||
if (d == null) return "";
|
|
||||||
Object dist = d.get("district");
|
|
||||||
if (dist != null && !dist.toString().isEmpty()) return dist.toString();
|
|
||||||
Object city = d.get("city");
|
|
||||||
if (city != null && !city.toString().isEmpty()) return city.toString();
|
|
||||||
Object prov = d.get("province");
|
|
||||||
return prov == null ? "" : prov.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回结构化地址:province / city / district / address(完整) / adcode。
|
|
||||||
* 失败时返回空 Map。
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getDetailedLocation(Double latitude, Double longitude) {
|
|
||||||
Map<String, Object> empty = new HashMap<>();
|
|
||||||
if (latitude == null || longitude == null) return empty;
|
|
||||||
if (!org.springframework.util.StringUtils.hasText(amapKey)) {
|
if (!org.springframework.util.StringUtils.hasText(amapKey)) {
|
||||||
log.warn("高德地图 key 未配置");
|
log.warn("高德地图 key 未配置");
|
||||||
return empty;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -72,32 +54,33 @@ public class FadAppLocationServiceImpl implements IFadAppLocationService {
|
|||||||
JSONObject response = restTemplate.getForObject(url.toString(), JSONObject.class);
|
JSONObject response = restTemplate.getForObject(url.toString(), JSONObject.class);
|
||||||
if (response == null || !"1".equals(response.getString("status"))) {
|
if (response == null || !"1".equals(response.getString("status"))) {
|
||||||
log.warn("高德逆地理编码失败: {}", response);
|
log.warn("高德逆地理编码失败: {}", response);
|
||||||
return empty;
|
return "";
|
||||||
}
|
}
|
||||||
JSONObject regeocode = response.getJSONObject("regeocode");
|
JSONObject regeocode = response.getJSONObject("regeocode");
|
||||||
if (regeocode == null) return empty;
|
if (regeocode == null) return "";
|
||||||
String formatted = regeocode.getString("formatted_address");
|
|
||||||
JSONObject ac = regeocode.getJSONObject("addressComponent");
|
JSONObject ac = regeocode.getJSONObject("addressComponent");
|
||||||
if (ac == null) return empty;
|
if (ac == null) return "";
|
||||||
|
|
||||||
Map<String, Object> out = new LinkedHashMap<>();
|
// 优先到区县级别
|
||||||
out.put("province", _str(ac, "province"));
|
String district = _str(ac, "district");
|
||||||
out.put("city", _str(ac, "city"));
|
String city = _str(ac, "city");
|
||||||
out.put("district", _str(ac, "district"));
|
String province = _str(ac, "province");
|
||||||
out.put("township", _str(ac, "township"));
|
if (!district.isEmpty()) {
|
||||||
out.put("adcode", _str(ac, "adcode"));
|
// 直辖市 city 为空时只显示区,否则拼"市+区"
|
||||||
out.put("address", formatted == null ? "" : formatted);
|
return city.isEmpty() ? district : (city + district);
|
||||||
return out;
|
}
|
||||||
|
if (!city.isEmpty()) return city;
|
||||||
|
return province;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("根据经纬度获取地址失败, lat={}, lng={}, err={}", latitude, longitude, e.getMessage());
|
log.warn("根据经纬度获取地址失败, lat={}, lng={}, err={}", latitude, longitude, e.getMessage());
|
||||||
return empty;
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 高德对部分字段(直辖市的 city、无下级的乡镇)返回 [],统一收敛成 "" */
|
||||||
private String _str(JSONObject o, String key) {
|
private String _str(JSONObject o, String key) {
|
||||||
Object v = o.get(key);
|
Object v = o.get(key);
|
||||||
if (v == null) return "";
|
if (v == null) return "";
|
||||||
// 高德对部分字段(如 city 是直辖市时)会返回空数组 []
|
|
||||||
if (v instanceof java.util.List && ((java.util.List<?>) v).isEmpty()) return "";
|
if (v instanceof java.util.List && ((java.util.List<?>) v).isEmpty()) return "";
|
||||||
return v.toString();
|
return v.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user