GetArea.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.ruoyi.system.utils;
  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.sun.javafx.collections.MappingChange;
  7. import java.math.BigDecimal;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. public class GetArea {
  11. private static final double A = 6378245.0;
  12. private static final double EE = 0.00669342162296594323;
  13. private static final double PI = 3.14159265358979324;
  14. private static final double X_PI = PI * 3000.0 / 180.0;
  15. //查询路线
  16. public JSONObject getluxian(String latlng1,String latlng2,String lang){
  17. String url = "https://maps.googleapis.com/maps/api/directions/json?"+
  18. "origin="+latlng1+
  19. "&destination="+latlng2+
  20. "&language="+lang+
  21. "&sensor=false&mode=driving"+
  22. "&key=AIzaSyBt-bYNprVqfpY3fPgoOj0RKbnC_g7ii1k";
  23. String res="";
  24. res = HttpUtil.get(url);
  25. JSONObject jsonObject = JSON.parseObject(res);
  26. return jsonObject;
  27. }
  28. public JSONObject getmpluxian(String latlng1,String latlng2){
  29. String url = "https://api.mapbox.com/directions/v5/mapbox/cycling/"+latlng1+";"+latlng2+"?access_token=pk.eyJ1IjoiY2l0eWV4cHJlc3MxNjg4IiwiYSI6ImNsd2xpcHBtZTA1dzIyanBvenBkcGI2aW0ifQ.azi4w1LoND2oYlJFkpFchg";
  30. String res="";
  31. res = HttpUtil.get(url);
  32. JSONObject jsonObject = JSON.parseObject(res);
  33. return jsonObject;
  34. }
  35. //获取电动车耗时
  36. public double getMotorbikeDuration(String latlng1, String latlng2) {
  37. String url = "https://api.mapbox.com/directions/v5/mapbox/driving/"+latlng1+";"+latlng2+"?access_token=pk.eyJ1IjoiY2l0eWV4cHJlc3MxNjg4IiwiYSI6ImNsd2xpcHBtZTA1dzIyanBvenBkcGI2aW0ifQ.azi4w1LoND2oYlJFkpFchg";
  38. String res = HttpUtil.get(url);
  39. JSONObject jsonObject = JSON.parseObject(res);
  40. double duration = jsonObject
  41. .getJSONArray("routes").getJSONObject(0).getDoubleValue("duration");
  42. // JSONArray steps = jsonObject
  43. // .getJSONArray("routes")
  44. // .getJSONObject(0)
  45. // .getJSONArray("legs")
  46. // .getJSONObject(0)
  47. // .getJSONArray("steps");
  48. double totalMotorbikeTime = duration*1.5; // 电动车总耗时(秒)
  49. //
  50. // for (int i = 0; i < ((JSONArray) steps).size(); i++) {
  51. // JSONObject step = steps.getJSONObject(i);
  52. // double distance = step.getDoubleValue("distance"); // 米
  53. // double speedKmh = getEVSpeed(step); // 获取电动车速度
  54. // double timeSec = distance / (speedKmh * 1000 / 3600);
  55. // totalMotorbikeTime += timeSec;
  56. //// step.put("ev_duration", timeSec); // 每段电动车耗时
  57. // }
  58. // jsonObject.put("ev_total_duration", totalMotorbikeTime); // 电动车总耗时(秒)
  59. return totalMotorbikeTime;
  60. }
  61. // 根据道路类型选择电动车速度
  62. private double getEVSpeed(JSONObject step) {
  63. JSONArray classes = step.getJSONArray("classes");
  64. if (classes != null && classes.contains("motorway")) return 50; // 高速/国道
  65. if (classes != null && classes.contains("primary")) return 40; // 主干道
  66. if (classes != null && classes.contains("secondary")) return 30; // 次干道
  67. if (classes != null && classes.contains("residential")) return 20; // 小路
  68. return 30; // 默认
  69. }
  70. public JSONObject getarearess(String latlng,String lang){
  71. String url = "https://api.map.baidu.com/reverse_geocoding/v3/?" +
  72. "ak=aYfBFMwOPlvztS60o6NdM9WdYbboAw1m" +
  73. "&output=json" +
  74. "&language="+lang+
  75. "&coordtype=wgs84ll" +
  76. "&extensions_poi=1"+
  77. "&radius=1000"+
  78. "&location="+latlng;
  79. String res="";
  80. res = HttpUtil.get(url);
  81. JSONObject jsonObject = JSON.parseObject(res);
  82. return jsonObject;
  83. }
  84. //谷歌附近地址
  85. public JSONObject getgoogle(String latlng,String lang){
  86. String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" +
  87. // "key=AIzaSyCeRnQtfpeH3skSoCvC6OZbEbwlIFTXvsY" +
  88. "key=AIzaSyBt-bYNprVqfpY3fPgoOj0RKbnC_g7ii1k" +
  89. "&language="+lang+
  90. "&radius=10000"+
  91. "&location="+latlng;
  92. String res="";
  93. res = HttpUtil.get(url);
  94. JSONObject jsonObject = JSON.parseObject(res);
  95. JSONObject org = new JSONObject();
  96. org.put("address",getgoogleadd(latlng,lang));
  97. org.put("results",jsonObject.getJSONArray("results"));
  98. return org;
  99. }
  100. //谷歌地址逆向
  101. public JSONObject getgoogleadd(String latlng,String lang){
  102. String url = "https://maps.googleapis.com/maps/api/geocode/json?" +
  103. "key=AIzaSyBt-bYNprVqfpY3fPgoOj0RKbnC_g7ii1k" +
  104. "&language="+lang+
  105. "&latlng="+latlng+
  106. "&location_type=ROOFTOP|RANGE_INTERPOLATED|GEOMETRIC_CENTER|APPROXIMATE";
  107. String res="";
  108. String lanss = lang=="en_US"?"Unknown address":lang=="vi"?"Địa chỉ vô danh":"无名地址";
  109. res = HttpUtil.get(url);
  110. System.out.println("谷歌地图getgoogleadd:"+res);
  111. JSONObject jsonObject = JSON.parseObject(res);
  112. JSONObject org = new JSONObject();
  113. JSONObject obj = (JSONObject) jsonObject.getJSONArray("results").get(0);
  114. org.put("name",jsonObject.getJSONObject("plus_code").getString("compound_code")==null?lanss:jsonObject.getJSONObject("plus_code").getString("compound_code"));
  115. org.put("address",obj.getString("formatted_address"));
  116. return org;
  117. }
  118. public JSONObject setlatlng(String latlng,Integer from,Integer to){
  119. String url = "https://api.map.baidu.com/geoconv/v1/?" +
  120. "ak=aYfBFMwOPlvztS60o6NdM9WdYbboAw1m" +
  121. "&output=json" +
  122. "&to=" + to +
  123. "&coords="+latlng+
  124. "&from="+from;
  125. String res="";
  126. res = HttpUtil.get(url);
  127. JSONObject jsonObject = JSON.parseObject(res);
  128. return jsonObject;
  129. }
  130. public String convertGCJ2WGS(double lng, double lat) {
  131. if (outOfChina(lng, lat)) {
  132. return lng + "," + lat;
  133. } else {
  134. double dLat = transformLat(lng - 105.0, lat - 35.0);
  135. double dLng = transformLng(lng - 105.0, lat - 35.0);
  136. double radLat = lat / 180.0 * PI;
  137. double magic = Math.sin(radLat);
  138. magic = 1 - EE * magic * magic;
  139. double sqrtMagic = Math.sqrt(magic);
  140. dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
  141. dLng = (dLng * 180.0) / (A / sqrtMagic * Math.cos(radLat) * PI);
  142. double mgLat = lat + dLat;
  143. double mgLng = lng + dLng;
  144. double wgsLng = lng * 2 - mgLng;
  145. double wgsLat = lat * 2 - mgLat;
  146. return wgsLng + "," + wgsLat;
  147. }
  148. }
  149. private static boolean outOfChina(double lng, double lat) {
  150. return (lng < 72.004 || lng > 137.8347) || (lat < 0.8293 || lat > 55.8271);
  151. }
  152. private static double transformLat(double x, double y) {
  153. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  154. ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
  155. ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
  156. ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
  157. return ret;
  158. }
  159. private static double transformLng(double x, double y) {
  160. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  161. ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
  162. ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
  163. ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
  164. return ret;
  165. }
  166. }