package com.chanlian.carbon.upms.account.utils;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chanlian.carbon.upms.account.vo.AddressVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* 根据地址获取经纬度
*
* @author yjj
* @create 2022/5/12 10:05
*/
@Component
public class AddressUtil {
private static final Logger log = LoggerFactory.getLogger(AddressUtil.class);
private static final String KEY = "***";
private static final String LBS_KEY = "***";
/**
* 根据地址获取经纬度
* @param addr
* @return
*/
public static String getLatAndLngByAddr(String addr) {
try {
String result = "";
addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
String queryStr = "http://api.tianditu.gov.cn/geocoder?ds=%7B'keyWord':'" + addr + "'%7D&tk=" + KEY;
String info = HttpClientUtil.get(queryStr);
if (StrUtil.isNotBlank(info)) {
JSONObject resultJson = JSON.parseObject(info);
if (!"无结果".equals(resultJson.get("msg"))) {
JSONObject locationObj = (JSONObject) resultJson.get("location");
//纬度
String lat = locationObj.get("lat") + "";
//经度
String lng = locationObj.get("lon") + "";
result = "经度:" + lng + "\n纬度:" + lat;
}
}
return result;
} catch (Exception e) {
return "无";
}
}
/**
* 根据经纬度获取地址
* @param longitude
* @param latitude
* @return
*/
public static AddressVo getAddrByLatAndLng(double longitude, double latitude) {
try {
String queryStr = "http://api.tianditu.gov.cn/geocoder?postStr={'lon':"+longitude+",'lat':"+latitude+",'ver':1}&type=geocode&tk=" + KEY;
URL url = new URL(queryStr);
/**
* 转义url中&,| 等特殊字符
*/
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
String info = HttpClientUtil.get(uri.toString());
if (StrUtil.isNotBlank(info)) {
JSONObject resultJson = JSON.parseObject(info);
if (!"无结果".equals(resultJson.get("msg"))) {
resultJson = (JSONObject) resultJson.get("result");
AddressVo addressVo = JSONUtil.toBean(resultJson.toJSONString(), AddressVo.class);
return addressVo;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据公司名称反查经纬度
* @param orgName 公司名称
* @param province 省
* @param city 市
* @param distinct 区/县
* @return map
* 经度 key为lng
* 纬度 key为lat
*/
public static Map<String, BigDecimal> regionalSearch(String orgName, String province, String city, String distinct) {
String region = province + "、" + city + "、" + distinct;
// &tag=公司企业
String queryStr = "https://api.map.baidu.com/place/v2/search?query=" + orgName + "®ion=" + region + "&output=json&ak=" + LBS_KEY + "&scope=2";
URL url = null;
Map<String, BigDecimal> resultMap = new HashMap<>();
try {
url = new URL(queryStr);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
String info = HttpClientUtil.get(uri.toString());
if (StrUtil.isNotBlank(info)) {
JSONObject resultJson = JSON.parseObject(info);
if (null != resultJson) {
Integer status = Integer.valueOf(String.valueOf(resultJson.get("status")));
//判断是否响应成功
if (0 == status) {
JSONArray results = (JSONArray) resultJson.get("results");
if(results.size()>0) {
JSONObject result = (JSONObject) results.get(0);
if (null != result) {
JSONObject location = (JSONObject) result.get("location");
if (null != location) {
resultMap.put("lng", (BigDecimal) location.get("lng"));
resultMap.put("lat", (BigDecimal) location.get("lat"));
}
}
}
}
}
}
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
return resultMap;
}
public static void main(String[] args) {
// System.out.println(getAddrByLatAndLng(120.216422d, 30.208839d));
Map<String, BigDecimal> stringStringMap = regionalSearch("浙江交投新能源投资有限公司", "浙江省", "台州市", "仙居县");
System.out.println(stringStringMap);
}
}
12-08
2727
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-15
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)