import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.beyond.reception.domain.vo.AreaDataVo;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
@Slf4j
public class AreaUtils {
private static final String TX_APIKEY = "";
private static final String GD_APIKEY = "";
private static final String BD_APIKEY = "";
public static AreaDataVo getAreaName(String latitude, String longitude) {
String urlStr = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + latitude + "," + longitude + "&get_poi=0&key=" + TX_APIKEY;
try {
URL url = new URL(urlStr);
log.info("腾讯地图坐标转换地址url开始:---------》{}", urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
log.info("腾讯地图坐标转换地址返回响应码:---------》{}", responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
log.info("腾讯地图坐标转换地址返回JSON数据:---------》{}", response);
JSONObject jsonObject = JSONUtil.parseObj(response.toString());
JSONObject result = JSONUtil.parseObj(jsonObject.get("result"));
JSONObject adInfo = JSONUtil.parseObj(result.get("ad_info"));
AreaDataVo vo = new AreaDataVo();
vo.setProvince(adInfo.get("province").toString());
vo.setCity(adInfo.get("city").toString());
vo.setDistrict(adInfo.get("district").toString());
return vo;
} else {
log.error("坐标转换地址有误,请联系管理员");
return null;
}
} catch (Exception e) {
log.error("坐标转换地址有误,请联系管理员:{}", e.getMessage());
}
return null;
}
public static AreaDataVo getAreaNameByAliyun(String latitude, String longitude) {
String urlStr = "https://restapi.amap.com/v3/geocode/regeo?location=" + latitude + "," + longitude + "&key=" + GD_APIKEY;
try {
URL url = new URL(urlStr);
log.info("高德地图坐标转换地址url开始:---------》{}", urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
log.info("高德地图坐标转换地址返回响应码:---------》{}", responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
log.info("高德地图坐标转换地址返回JSON数据:---------》{}", response);
JSONObject jsonObject = JSONUtil.parseObj(response.toString());
JSONObject result = JSONUtil.parseObj(jsonObject.get("regeocode"));
JSONObject adInfo = JSONUtil.parseObj(result.get("addressComponent"));
AreaDataVo vo = new AreaDataVo();
String[] cities = {"上海市", "重庆市", "北京市", "天津市"};
if (Arrays.asList(cities).contains(adInfo.get("province").toString())) {
vo.setProvince(adInfo.get("province").toString());
vo.setCity(adInfo.get("province").toString());
vo.setDistrict(adInfo.get("district").toString());
} else {
vo.setProvince(adInfo.get("province").toString());
vo.setCity(adInfo.get("city").toString());
vo.setDistrict(adInfo.get("district").toString());
}
return vo;
} else {
log.error("坐标转换地址有误,请联系管理员");
return null;
}
} catch (Exception e) {
log.error("坐标转换地址有误,请联系管理员:{}", e.getMessage());
}
return null;
}
public static AreaDataVo getAreaNameByBaidu(String latitude, String longitude) {
String urlStr = "https://api.map.baidu.com/reverse_geocoding/v3/?location=" + latitude + "," + longitude + "&extensions_poi=0&output=json&ak=" + BD_APIKEY;
try {
URL url = new URL(urlStr);
log.info("百度地图坐标转换地址url开始:---------》{}", urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
log.info("百度地图坐标转换地址返回响应码:---------》{}", responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
log.info("百度地图坐标转换地址返回JSON数据:---------》{}", response);
JSONObject jsonObject = JSONUtil.parseObj(response.toString());
JSONObject result = JSONUtil.parseObj(jsonObject.get("result"));
JSONObject adInfo = JSONUtil.parseObj(result.get("addressComponent"));
AreaDataVo vo = new AreaDataVo();
vo.setProvince(adInfo.get("province").toString());
vo.setCity(adInfo.get("city").toString());
vo.setDistrict(adInfo.get("district").toString());
return vo;
} else {
log.error("坐标转换地址有误,请联系管理员");
return null;
}
} catch (Exception e) {
log.error("坐标转换地址有误,请联系管理员:{}", e.getMessage());
}
return null;
}
}
import lombok.Data;
import java.io.Serializable;
@Data
public class AreaDataVo implements Serializable {
private String province;
private String city;
private String district;
}