小程序经纬度接口,临时主页接口
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user