ProvinceUtils.java 6.0 KB

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