1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
| package msdev.test.util;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import msdev.test.constants.GaoDeWebConstants; import org.apache.commons.lang.StringUtils; import java.math.BigDecimal; import java.util.*;
public class GaoDeMapUtil { public static void main(String[] args) {
String locations = ""; int count = 830; double longitude = 116.481499; double latitude = 39.990475;
for (int i = 0; i < count; i++) { longitude = new BigDecimal(longitude).add(BigDecimal.valueOf(0.000001)).doubleValue(); latitude = new BigDecimal(latitude).add(BigDecimal.valueOf(0.000001)).doubleValue(); locations += (i == count - 1 ? longitude + "," + latitude : longitude + "," + latitude + "|"); }
String convertGps = convert(locations, "gps");
System.out.println("gps转换后的高德坐标:" + convertGps);
String[] split = convertGps.split(";");
System.out.println("split0" + split[0]);
List<String> locationList = new ArrayList<>(Arrays.asList(split));
System.out.println("size:" + locationList.size());
System.out.println(locationList);
}
public static HttpResponse getGeo(String address) {
HashMap<String, String> params = new HashMap<>(); params.put("key", GaoDeWebConstants.API_KEY); params.put("address", address);
String parmaStr = buildParamString(params);
String apiUrl = GaoDeWebConstants.GEO_URL + "?" + parmaStr;
return HttpRequest.get(apiUrl).execute(); }
public static HttpResponse getReGeo(double longitude, double latitude) {
HashMap<String, String> params = new HashMap<>(); params.put("key", GaoDeWebConstants.API_KEY); params.put("location", longitude + "," + latitude);
String parmaStr = buildParamString(params);
String apiUrl = GaoDeWebConstants.REGEO_URL + "?" + parmaStr;
return HttpRequest.get(apiUrl).execute(); }
public static String getGps(String address) { HttpResponse response = getGeo(address); System.out.println(response); if (response != null && response.isOk()) {
JSONObject resObj = JSONObject.parseObject(response.body()); JSONArray geocodes = resObj.getJSONArray("geocodes"); if (geocodes == null || geocodes.isEmpty()) { return ""; }
return geocodes.getJSONObject(0).getString("location"); } return "";
}
public static String getLocation(double longitude, double latitude) { HttpResponse response = getReGeo(longitude, latitude); if (response != null && response.isOk()) {
JSONObject resObj = JSONObject.parseObject(response.body()); JSONObject regeocode = resObj.getJSONObject("regeocode"); if (regeocode == null || regeocode.isEmpty()) { return ""; }
return regeocode.getString("formatted_address"); } return "";
}
public static String convert(String locations, String coordsys) { HashMap<String, String> params = new HashMap<>(); params.put("key", GaoDeWebConstants.API_KEY); params.put("locations", locations); params.put("coordsys", coordsys);
String parmaStr = buildParamString(params);
parmaStr = parmaStr.replace("|", "%7C");
String apiUrl = GaoDeWebConstants.CONVERT_URL + "?" + parmaStr;
HttpResponse response = HttpRequest.get(apiUrl).execute(); if (response != null && response.isOk()) { JSONObject resObj = JSONObject.parseObject(response.body()); return resObj.getString("locations"); } return "";
}
public static String convertTest(String locations, String coordsys) { String[] split = locations.split("\\|"); int length = split.length;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) { stringBuilder.append("87.607277560764,43.809631076389").append(";"); }
String str = stringBuilder.toString();
if (str.endsWith(";")) { str = str.substring(0, str.length() - 1); }
return str;
}
private static String buildParamString(Map<String, String> params) { StringBuilder paramBuilder = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { if (paramBuilder.length() > 0) { paramBuilder.append("&"); } paramBuilder.append(entry.getKey()).append("=").append(entry.getValue()); } return paramBuilder.toString(); }
private static String parseLocation(String responseBody) { return responseBody; }
public static HttpResponse getReGeo(String location) {
HashMap<String, String> params = new HashMap<>(); params.put("key", GaoDeWebConstants.API_KEY); params.put("location", location);
String parmaStr = buildParamString(params);
String apiUrl = GaoDeWebConstants.REGEO_URL + "?" + parmaStr;
return HttpRequest.get(apiUrl).execute(); }
public static Map<String, String> getAddressInfoByGps(Double longitude, Double latitude) { Map<String, String> result = new LinkedHashMap<>();
if (longitude == null || latitude == null) { return result; }
String location = longitude + "," + latitude;
String gdLocation = convert(location, "gps");
return getAddressInfo(gdLocation); }
public static Map<String, String> getAddressInfo(String gdLocation) { Map<String, String> result = new LinkedHashMap<>();
HttpResponse response = getReGeo(gdLocation); if (response != null && response.isOk()) {
JSONObject resObj = JSONObject.parseObject(response.body()); JSONObject regeocode = resObj.getJSONObject("regeocode"); if (regeocode == null || regeocode.isEmpty()) { return result; }
String address = regeocode.getString("formatted_address"); String country = "", province = "", city = "", district = "";
result.put("address", address);
JSONObject addressComponent = regeocode.getJSONObject("addressComponent"); if (addressComponent != null && !addressComponent.isEmpty()) { country = addressComponent.getString("country"); province = addressComponent.getString("province"); district = addressComponent.getString("district");
city = addressComponent.getString("city").replace("[]", "");
if (StringUtils.isBlank(city) && StringUtils.isNotBlank(province)) { city = province; }
}
result.put("country", country); result.put("province", province); result.put("city", city); result.put("district", district); }
return result;
}
public static Map<String, String> getAddressInfoTest(String gdLocation) { Map<String, String> result = new LinkedHashMap<>(); result.put("address", "新疆维吾尔自治区乌鲁木齐市水磨沟区新民路街道红山公园红山公园北园"); result.put("country", "中国"); result.put("province", "新疆维吾尔自治区"); result.put("city", "乌鲁木齐市"); result.put("district", "水磨沟区"); return result;
}
public static List<Map<String, String>> processConvert(List<Map<String, Object>> batchData) { List<Map<String, String>> result = new ArrayList<>(); List<Map<String, Object>> convert = new ArrayList<>(); StringBuilder stringBuilder = new StringBuilder();
for (Map<String, Object> gps : batchData) { String guid = (String) gps.get("guid"); String gpsGuid = (String) gps.get("gpsGuid");
try { Double longitude = (Double) gps.get("longitude"); Double latitude = (Double) gps.get("latitude");
if (StringUtils.isNotBlank(guid) && StringUtils.isNotBlank(gpsGuid) && longitude != null && latitude != null) {
String location = longitude + "," + latitude; stringBuilder.append(location).append("|");
gps.put("origin", location); convert.add(gps); } } catch (Exception e) { e.printStackTrace(); System.out.println("Error: guid :[" + guid + "],gpsGuid:[" + gpsGuid + "]"); } }
String locations = stringBuilder.toString(); if (locations.endsWith("|")) { locations = locations.substring(0, locations.length() - 1); }
System.out.println("locations:" + locations);
if (StringUtils.isBlank(locations)) { return result; }
String gdLocations = convert(locations, "gps");
String[] split = gdLocations.split(";");
List<String> locationList = new ArrayList<>(Arrays.asList(split));
for (int i = 0; i < locationList.size(); i++) { HashMap<String, String> convertData = new HashMap<>(); convertData.put("location", locationList.get(i)); convertData.put("guid", (String) convert.get(i).get("guid")); convertData.put("gpsGuid", (String) convert.get(i).get("gpsGuid")); convertData.put("origin", (String) convert.get(i).get("origin")); result.add(convertData); }
return result;
}
public static List<Map<String, String>> batchConvert(List<Map<String, Object>> gpsInfo, int batchSize) { ArrayList<Map<String, String>> datas = new ArrayList<>();
if (gpsInfo == null || gpsInfo.isEmpty()) { return datas; }
if (batchSize > 40) { batchSize = 40; }
int totalDataSize = gpsInfo.size(); int totalBatches = (int) Math.ceil((double) totalDataSize / batchSize);
int i = 1; for (int batchNumber = 0; batchNumber < totalBatches; batchNumber++) {
System.out.println("第" + i + "次处理 Start"); int startIndex = batchNumber * batchSize; int endIndex = Math.min(startIndex + batchSize, totalDataSize);
List<Map<String, Object>> batchData = gpsInfo.subList(startIndex, endIndex);
List<Map<String, String>> itemData = processConvert(batchData); System.out.println("第" + i + "次处理结果:" + itemData);
datas.addAll(itemData); i++; }
System.out.println("批量处理结束,最终处理结果:" + datas);
return datas;
}
}
|