package com.sinata.zuul.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 高德地图的地理编码工具类
|
*/
|
@Component
|
public class GDMapGeocodingUtil {
|
|
private String key = "918be06295ade4ef9c234b76c463c7d7";
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
|
/**
|
* 将行政区域名称转化为坐标
|
* @param province
|
* @param city
|
* @param county
|
* @param address
|
* @return
|
*/
|
public Map<String, Object> geocoding(String province, String city, String county, String address){
|
Map<String, Object> map = new HashMap<>();
|
if(StringUtil.isEmpty(province)){
|
map.put("status", -1);
|
map.put("data", "省不能为空");
|
return map;
|
}
|
if((StringUtil.isEmpty(city) && StringUtil.isNotEmpty(county)) || (StringUtil.isEmpty(city) && StringUtil.isNotEmpty(address))){
|
map.put("status", -1);
|
map.put("data", "市不能为空");
|
return map;
|
}
|
if((StringUtil.isEmpty(county) && StringUtil.isNotEmpty(address))){
|
map.put("status", -1);
|
map.put("data", "县/区不能为空");
|
return map;
|
}
|
|
String url = "https://restapi.amap.com/v3/geocode/geo?key=" + key + "&output=JSON";
|
url += "&address=" + province + (StringUtil.isNotEmpty(city) ? city : "") + (StringUtil.isNotEmpty(county) ? county : "") + (StringUtil.isNotEmpty(address) ? address : "");
|
String forObject = restTemplate.getForObject(url, String.class);
|
JSONObject jsonObject = JSON.parseObject(forObject);
|
String status = jsonObject.getString("status");
|
List<String> list = new ArrayList<>();
|
if(status.equals("1")){
|
JSONArray geocodes = jsonObject.getJSONArray("geocodes");
|
for(int i = 0; i < geocodes.size(); i++){
|
String location = geocodes.getJSONObject(i).getString("location");
|
list.add(location);
|
}
|
}
|
map.put("status", 0);
|
map.put("data", list);
|
return map;
|
}
|
|
|
/**
|
* 根据经纬度获取行政区域信息
|
* @param lon
|
* @param lan
|
* @return
|
* @throws Exception
|
*/
|
public Map<String, String> geocode(String lon, String lan) throws Exception{
|
String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan;
|
String forObject = restTemplate.getForObject(url, String.class);
|
JSONObject jsonObject = JSON.parseObject(forObject);
|
Map<String, String> map = new HashMap<>();
|
if(jsonObject.getString("status").equals("1")){
|
JSONObject regeocode = jsonObject.getJSONObject("regeocode");
|
JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
|
String address = regeocode.getString("formatted_address");
|
map.put("address", address);
|
String code = addressComponent.getString("adcode");
|
String province = addressComponent.getString("province");
|
String city = addressComponent.getString("city");
|
String district = addressComponent.getString("district");
|
map.put("province", province);
|
map.put("provinceCode", code.substring(0, 2) + "0000");
|
map.put("city", city);
|
map.put("cityCode", code.substring(0, 4) + "00");
|
map.put("district", district);
|
map.put("districtCode", code);
|
}
|
return map;
|
}
|
}
|