高德WEB服务API地址:https://lbs.amap.com/api/webservice/summary">https://lbs.amap.com/api/webservice/summary

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
package msdev.test.constants;

/**
* Created By shiguang On 2024/1/23 10:57
* <p>
* 高德开放平台:<a href="https://lbs.amap.com/api/webservice/summary">https://lbs.amap.com/api/webservice/summary</a>
* <p>
* 接口请求方式: GET
*/
public class GaoDeWebConstants {
/**
* 高德地图开发者API_KEY
*/
public static final String API_KEY = "YOUR_API_KEY";


/**
* 数字签名私钥
*/
public static final String SIGN_PRIVATE_KEY = "2c4bc8e8c4c018de3080816050397986";


/**
* 地理编码API服务地址(根据地理位置获取经纬度)
*/
public static final String GEO_URL = "https://restapi.amap.com/v3/geocode/geo";

/**
* 逆地理编码API服务地址(根据经纬度获取地址位置)
*/
public static final String REGEO_URL = "https://restapi.amap.com/v3/geocode/regeo";

/**
* 坐标转换API服务地址
* 将非高德坐标(GPS坐标、mapbar坐标、baidu坐标)转换成高德坐标
*/
public static final String CONVERT_URL = "https://restapi.amap.com/v3/assistant/coordinate/convert";

/**
* 静态地图API服务地址
* 回一张地图图片响应HTTP请求,使用户能够将高德地图以图片形式嵌入自己的网页中。
* 用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物,如标签、标注、折线、多边形。
*/
public static final String STATIC_MAP_URL = "https://restapi.amap.com/v3/staticmap";


/**
* 距离测量API服务地址
*/
public static final String DISTANCE_URL = "https://restapi.amap.com/v3/distance";
}

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.*;

/**
* Created By shiguang On 2024/1/23 11:08
*/
public class GaoDeMapUtil {
public static void main(String[] args) {
// double longitude = 121.4737; // 经度
// double latitude = 31.2304; // 纬度
//
// String address = "北京市朝阳区阜通东大街6号";
//
// String location = getLocation(longitude, latitude);
// System.out.println(location);
//
// String gps = getGps(address);
//
// System.out.println("根据地址获取到的gps位置" + gps);

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 + "|");
}

// System.out.println(locations);


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);


// //上海市浦东新区金桥镇金粤路地铁站4号口怡亚通科技广场(锦绣东路)
// double longitude = 121.62411975834402; // 经度
// double latitude = 31.244654540707472; // 纬度
//
// // 安徽省合肥市包河区骆岗街道黄河路骆岗公园
//// double longitude = 117.30322262272239; // 经度
//// double latitude = 31.76787255014206; // 纬度
//
// Map<String, String> addressInfo = getAddressInfoByGps(longitude, latitude);
//
// System.out.println(addressInfo);


}


/**
* 获取
*
* @param address 结构化地址信息
* 规则遵循:国家、省份、城市、区县、城镇、乡村、街道、门牌号码、屋邨、大厦,如:北京市朝阳区阜通东大街6号。
*/
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;

// 发送 GET 请求
return HttpRequest.get(apiUrl).execute();
}

/**
* 获取逆地理编码(根据经纬度获取地址位置)
*
* @param longitude 经度
* @param latitude 纬度
*/

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;

// 发送 GET 请求
return HttpRequest.get(apiUrl).execute();
}


/**
* 获取gps经纬度信息(如获取到多个则返回第一个)
*/
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 "";

}

/**
* 根据经纬度获取地理位置
*
* @param longitude 经度
* @param latitude 纬度
* @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 "";

}

/**
* 坐标转换,将非高德坐标(GPS坐标、mapbar坐标、baidu坐标)转换成高德坐标。
*
* @param locations 坐标点,经度和纬度用“,”分割,经度在前,纬度在后,经纬度小数点后不得超过6位。多个坐标对之间用“|”进行分隔最多支持40对坐标。
* 如:116.481499,39.990475|116.481499,39.990375
* @param coordsys 原坐标系,可选值:gps;mapbar;baidu;autonavi(不进行转换) 默认autonavi
* @return 转换后的高德坐标 例如:116.487585177952,39.991754014757;116.487585177952,39.991653917101
*/
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;

// 发送 GET 请求
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) {
// 在实际应用中,您需要根据高德地图返回的 JSON 数据解析出您需要的地理位置信息
// 这里只是一个简单的演示,实际中可能需要使用 JSON 解析库进行处理
return responseBody;
}


/**
* 获取逆地理编码(根据经纬度获取地址位置)
*
* @param location 高德坐标
*/

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;

// 发送 GET 请求
return HttpRequest.get(apiUrl).execute();
}


/**
* 根据GPS经纬度获取位置信息
*
* @param longitude 经度
* @param latitude 纬度
* @return 当前坐标精确位置及所处国家、省份、市、区 Map集合
*/
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;

//将gps坐标转换为高德坐标
String gdLocation = convert(location, "gps");

return getAddressInfo(gdLocation);
}


/**
* 根据GPS经纬度获取位置信息
*
* @param gdLocation 高德坐标
* @return 当前坐标精确位置及所处国家、省份、市、区 Map集合
*/
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 为空数组,非直辖市为文本
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;
}

//将gps坐标转换为高德坐标
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;

}

/**
* 批量转换gps坐标
*
* @param gpsInfo 待转换gps数据集合,需包含数据id,经度(longitude),纬度(latitude)
* @param batchSize 每批次处理的数据量,最大为40
*/
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;
}

// 高德地图一次最多支持40对坐标转换
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;

}


}