ProvinceUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.fdkankan.manage.util;
  2. import cn.hutool.http.HttpUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fdkankan.manage.common.ResultCode;
  7. import com.fdkankan.manage.exception.BusinessException;
  8. import com.fdkankan.manage.vo.response.AddressComponent;
  9. import com.fdkankan.manage.vo.response.IpAddressVo;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.apache.commons.lang3.StringUtils;
  12. import java.io.UnsupportedEncodingException;
  13. import java.net.URL;
  14. import java.net.URLEncoder;
  15. import java.util.Objects;
  16. @Slf4j
  17. public class ProvinceUtils {
  18. private static String getProvince(String log, String lat ){
  19. //lat 小 log 大
  20. //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
  21. String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";
  22. String res = "";
  23. try {
  24. URL url = new URL(urlString);
  25. java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
  26. conn.setDoOutput(true);
  27. conn.setRequestMethod("POST");
  28. java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
  29. String line;
  30. while ((line = in.readLine()) != null) {
  31. res += line+"\n";
  32. }
  33. in.close();
  34. } catch (Exception e) {
  35. System.out.println("error in wapaction,and e is " + e.getMessage());
  36. }
  37. System.out.println(res);
  38. JSONObject jsonObject = JSONObject.parseObject(res);
  39. JSONArray jsonArray = JSON.parseArray(jsonObject.getString("addrList"));
  40. JSONObject jsonObject1 = jsonArray.getJSONObject(0);
  41. String arr[] = jsonObject1.get("admName").toString().split(",");
  42. System.out.println(arr[0]);
  43. return arr[0];
  44. }
  45. /**
  46. * 根据经纬度转地址
  47. * @param points
  48. * @return
  49. */
  50. public static String amapKey = "3609daa52e8ae4493393292213e2fb98";
  51. //经度和纬度用","分割,经度在前,纬度在后,经纬度小数点后不得超过6位。多个坐标对之间用”|”进行分隔最多支持40对坐标。
  52. public static AddressComponent pointsToLocationsAll(String points) {
  53. //将GPS坐标转化为高德地图坐标的URL后再去请求位置信息
  54. try {
  55. points = URLEncoder.encode(points,"UTF-8");
  56. String convertUrl =
  57. "https://restapi.amap.com/v3/assistant/coordinate/convert?locations="+points+"&coordsys=gps&key="+amapKey;
  58. //GPS坐标转为高德地图坐标
  59. String s = HttpUtil.get(convertUrl);
  60. JSONObject jsonObject = JSON.parseObject(s);
  61. String status = (String) jsonObject.get("status");
  62. if(Objects.equals(status,"0")){
  63. throw new RuntimeException("远程调用经纬度转化出错");
  64. }
  65. String locations = (String) jsonObject.get("locations");
  66. String formattedAmapPoints = null;
  67. try {
  68. formattedAmapPoints = URLEncoder.encode(locations.replaceAll(";", "|"), "UTF-8");
  69. } catch (UnsupportedEncodingException e) {
  70. throw new RuntimeException(e.getMessage());
  71. }
  72. String locationUrl = "https://restapi.amap.com/v3/geocode/regeo?output=json&location="+formattedAmapPoints+"&key="+amapKey+"&radius=1000&batch=true";
  73. String s1 = HttpUtil.get(locationUrl);
  74. System.out.println(s1);
  75. //获取转地址后的结果
  76. JSONObject parseObject = JSON.parseObject(s1);
  77. String status1 = (String) parseObject.get("status");
  78. if(Objects.equals(status1,"0")){
  79. throw new RuntimeException("根据经纬度获取具体地址出错");
  80. }
  81. JSONArray regeocodes = parseObject.getJSONArray("regeocodes");
  82. String formattedAddress = "";
  83. AddressComponent addressComponent = null;
  84. for (Object regeocode : regeocodes) {
  85. JSONObject object = (JSONObject) regeocode;
  86. formattedAddress = (String)object.get("formatted_address");
  87. JSONObject jsonObject1 = (JSONObject) object.get("addressComponent");
  88. addressComponent = JSONObject.toJavaObject(jsonObject1, AddressComponent.class);
  89. }
  90. log.info("经纬度【{}】转化为具体地点【{}】",points,formattedAddress);
  91. return addressComponent;
  92. } catch (Exception e) {
  93. log.info("经纬度转换错误error:{}",e);
  94. }
  95. return null;
  96. }
  97. public static String getAddressByIpHost ="https://restapi.amap.com/v3/ip?ip=%s&key=%s";
  98. public static IpAddressVo getAddressByIp(String ip){
  99. //GPS坐标转为高德地图坐标
  100. try {
  101. String s = HttpUtil.get(String.format(getAddressByIpHost,ip,amapKey));
  102. JSONObject jsonObject = JSON.parseObject(s);
  103. IpAddressVo javaObject = JSONObject.toJavaObject(jsonObject, IpAddressVo.class);
  104. if(StringUtils.isNotBlank(javaObject.getProvince())){
  105. javaObject.setCountry("中国");
  106. }
  107. return javaObject;
  108. }catch (Exception e){
  109. log.info("ip地址转换地址失败:{},{}",ip,e);
  110. }
  111. return new IpAddressVo();
  112. }
  113. public static void main(String[] args) {
  114. System.out.println( getAddressByIp("127"));
  115. }
  116. }