AmapManager.java
6.96 KB
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
package cn.com.glsx.entmicro.manager;
import cn.com.glsx.entmicro.config.AmapProperty;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.oreframework.util.http.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @Title: AmapManager.java
* @Description: 高德地图接口管理
* @author Alvin
* @date 2019年9月5日 下午3:11:02
* @version V1.0
* @Company: didihu.com.cn
* @Copyright Copyright (c) 2018
*/
@Slf4j
@Component
public class AmapManager {
Logger logger = Logger.getLogger(getClass());
@Autowired
private AmapProperty property;
static final String STATUSTYPE = "1";
static final String BAIDU_STATUSTYPE = "0";
static final String RESULTE_INFO = "OK";
static final String OUTPUT = "JSON";
static final String BAIDU_OUTPUT = "json";
static final String EXTENSIONS_ALL = "all";
static final String COORDTYPE = "bd09ll";
static final String FROM = "3";
static final String TO = "5";
public String getAddrFromLngLat(String lng, String lat) {
String[] strings = convertCoordinates(lng, lat);
if (strings == null || strings.length == 0) {
log.info("百度坐标转换失败");
return "";
}
Map<String, String> params = new HashMap<>(10);
params.put("location", strings[0] + "," + strings[1]);
params.put("coordtype", COORDTYPE);
params.put("ak", property.getBaiduKey());
params.put("output", BAIDU_OUTPUT);
params.put("extensions_poi", "1");
String response = null;
try {
response = HttpUtil.post(property.getBaiduGeocodeUrl(), params);
} catch (IOException e) {
log.error("通过经纬度获取详细地址异常:" + e.getMessage() + e);
}
if (response == null) {
log.info("返回地址为空");
return "";
}
log.info("通过经纬度获取详细地址:" + response);
JSONObject json = JSONObject.fromObject(response);
String status = json.getString("status");
log.info("解析地址的状态为:" + status);
String address = null;
if (BAIDU_STATUSTYPE.equals(status)) {
try {
JSONObject result = JSONObject.fromObject(json.get("result"));
address = result.getString("formatted_address_poi");
} catch (Exception e) {
log.info("解析地址数据异常:{}", json);
return null;
}
}
log.info("原始坐标:{},百度坐标:{},address:{}", lng + "," + lat, strings[0] + "," + strings[1], address);
return address;
}
public String[] convertCoordinates(String lat, String lng) {
Map<String, String> params = new HashMap<>(10);
params.put("coords", lng + "," + lat);
params.put("ak", property.getBaiduKey());
params.put("from", FROM);
params.put("to", TO);
String response = null;
try {
response = HttpUtil.post(property.getGeoconvUrl(), params);
} catch (IOException e) {
log.error("百度坐标转换异常:" + e.getMessage() + e);
}
if (response == null) {
log.info("百度坐标返回为空");
return null;
}
JSONObject json = JSONObject.fromObject(response);
String status = json.getString("status");
log.info("解析百度坐标的状态为:" + status);
String[] coordinates = new String[2];
if (BAIDU_STATUSTYPE.equals(status)) {
try {
JSONObject result = json.getJSONArray("result").getJSONObject(0);
coordinates[0] = result.getString("x");
coordinates[1] = result.getString("y");
} catch (Exception e) {
log.info("百度坐标数据异常:{}", json);
return null;
}
}
return coordinates;
}
/**
* @param lng:经度,lat:纬度
* @return 地址
* @description 根据经纬度查地址
* @author leiyj
* @date 2019年1月14日
*/
public String getAddrFromLngLatFromGaode(String lng, String lat) {
Map<String, String> params = new HashMap<String, String>(10);
params.put("location", lng + "," + lat);
params.put("output", OUTPUT);
params.put("key", property.getKey());
params.put("extensions", EXTENSIONS_ALL);
String result = null;
try {
result = HttpUtil.post(property.getGeocodeUrl(), params);
} catch (IOException e) {
logger.error("通过经纬度获取详细地址异常:" + e.getMessage() + e);
}
if (result == null) {
logger.info("返回地址为空");
return "";
}
JSONObject json = JSONObject.fromObject(result);
String status = json.getString("status");
logger.info("解析地址的状态为:" + status);
String address = null;
if (STATUSTYPE.equals(status)) {
JSONObject regeocode = JSONObject.fromObject(json.get("regeocode"));
address = regeocode.getString("formatted_address");
}
return address;
}
/**
* @param origins:起点经纬度,destination:终点经纬度
* @return 地址
* @description 根据起点和终点经纬度查询距离
* @author leiyj
* @date 2019年1月14日
*/
public Double getDistance(String origins, String destination) {
Map<String, String> params = new HashMap<String, String>(10);
params.put("origins", origins);
params.put("destination", destination);
params.put("output", OUTPUT);
params.put("key", property.getKey());
params.put("type", "0");
String result = null;
try {
result = HttpUtil.post(property.getDistanceUrl(), params);
} catch (IOException e) {
logger.error("经纬度计算距离异常:" + e.getMessage() + e);
}
if (result == null) {
logger.info("返回计算距离为空");
return 0D;
}
Double distance = 0D;
logger.info(result);
JSONObject json = JSONObject.fromObject(result);
String status = json.getString("status");
String info = json.getString("info");
// logger.info("据起点和终点经纬度查询返回状态:" + status);
if (STATUSTYPE.equals(status)) {
JSONArray results = JSONArray.fromObject(json.get("results"));
String r = results.getString(0);
JSONObject json1 = JSONObject.fromObject(r);
distance = Double.parseDouble(json1.getString("distance"));
logger.info("俩地相隔的距离:" + distance + "米");
}
return distance / 1000;
}
}