小程序经纬度接口,临时主页接口

This commit is contained in:
张松
2025-02-14 14:39:15 +08:00
parent 0efbb1e747
commit b8f5448a7b
6 changed files with 387 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
package com.czg.service.account.service.impl;
import com.alibaba.fastjson2.JSONObject;
import com.czg.account.service.GeoService;
import com.czg.exception.ApiNotPrintException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import java.util.Objects;
/**
* 经纬度
*
* @author Administrator
*/
@Service
@Slf4j
public class GeoServiceImpl implements GeoService {
private final RestClient restClient = RestClient.create();
private static final String API_KEY = "7a7f2e4790ea222660a027352ee3af39";
private static final String BASE_URL = "https://restapi.amap.com/v3/geocode/regeo";
@Override
public JSONObject getAddress(String lat, String lng) {
// 高德 API 坐标格式为 "经度,纬度"
String location = lng + "," + lat;
String response = restClient.get()
.uri(BASE_URL + "?key=" + API_KEY + "&location=" + location)
.retrieve()
.body(String.class);
JSONObject jsonObject = JSONObject.parseObject(response);
if (jsonObject == null) {
log.warn("经纬度获取失败{}", response);
throw new ApiNotPrintException("获取经纬度失败");
}
return jsonObject.getJSONObject("regeocode");
}
}