提交 | 用户 | age
|
03d225
|
1 |
package com.yc.sdk.huaweimap.service; |
F |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
|
4 |
import com.yc.entity.AttachmentConfig; |
|
5 |
import com.yc.exception.ApplicationException; |
|
6 |
import com.yc.sdk.huaweimap.entity.*; |
|
7 |
import com.yc.sdk.map.entity.LocationEntity; |
a61f53
|
8 |
import com.yc.sdk.map.entity.SearchEntity; |
03d225
|
9 |
import com.yc.sdk.map.service.AbstractMapService; |
F |
10 |
import org.apache.commons.lang3.StringUtils; |
|
11 |
import org.apache.http.HttpResponse; |
|
12 |
import org.apache.http.HttpStatus; |
|
13 |
import org.apache.http.client.HttpClient; |
|
14 |
import org.apache.http.client.methods.HttpPost; |
|
15 |
import org.apache.http.entity.StringEntity; |
|
16 |
import org.apache.http.impl.client.HttpClients; |
|
17 |
import org.apache.http.util.EntityUtils; |
|
18 |
import org.springframework.stereotype.Service; |
|
19 |
|
|
20 |
/** |
|
21 |
* 华为地图服务 |
|
22 |
*/ |
|
23 |
@Service |
|
24 |
public class HuaWeiMapService implements AbstractMapService { |
|
25 |
|
|
26 |
private static String KEY = AttachmentConfig.get("HuaWeiWebServiceKey"); |
|
27 |
private static String GEOCODEURL = "https://siteapi.cloud.huawei.com/mapApi/v1/siteService/reverseGeocode"; |
|
28 |
private static String IPURL = "https://openlocation-drcn.platform.dbankcloud.com/networklocation/v1/ipLocation"; |
a61f53
|
29 |
private static String SEARCHURL = "https://siteapi.cloud.huawei.com/mapApi/v1/siteService/nearbySearch"; |
a2ecbf
|
30 |
private static String SEARCHBYTEXTURL = "https://siteapi.cloud.huawei.com/mapApi/v1/siteService/searchByText"; |
03d225
|
31 |
|
F |
32 |
@Override |
|
33 |
public LocationEntity getIPLocation(String ip) throws Exception { |
|
34 |
HttpClient httpClient = HttpClients.createDefault(); |
|
35 |
HttpPost post = new HttpPost(IPURL); |
|
36 |
post.setHeader("Content-Type", "application/json;charset=utf-8"); |
|
37 |
post.setHeader("x-forwarded-for", ip); |
|
38 |
post.setHeader("Authorization", "Bearer " + KEY); |
|
39 |
String data = "{\"ip\":\"" + ip + "\"}"; |
|
40 |
StringEntity myEntity = new StringEntity(data, "UTF-8"); |
|
41 |
myEntity.setContentType("application/json;charset=UTF-8"); |
|
42 |
post.setEntity(myEntity); |
|
43 |
HttpResponse response = httpClient.execute(post); |
|
44 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
45 |
if (statusCode != HttpStatus.SC_OK) { |
|
46 |
throw new ApplicationException("获取响应失败,请重新提交-" + response.getStatusLine().getStatusCode() + "[" + response.getStatusLine().getReasonPhrase() + "]"); |
|
47 |
} else { |
|
48 |
IPLocationResponseEntity responseEntity = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"), IPLocationResponseEntity.class); |
|
49 |
if ("0".equalsIgnoreCase(responseEntity.getStatus())) { |
|
50 |
//成功 |
|
51 |
return responseEntity.getResult(); |
|
52 |
} else { |
|
53 |
throw new ApplicationException(responseEntity.getErrorMsg()); |
|
54 |
} |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
a2ecbf
|
59 |
public Object searchByText(SearchEntity searchEntity) throws Exception { |
F |
60 |
try { |
|
61 |
if (StringUtils.isBlank(searchEntity.getQuery())) { |
|
62 |
throw new ApplicationException("搜索关键字不能为空"); |
|
63 |
} |
|
64 |
HttpClient httpClient = HttpClients.createDefault(); |
|
65 |
HttpPost post = new HttpPost(SEARCHBYTEXTURL); |
|
66 |
post.setHeader("Content-Type", "application/json;charset=utf-8"); |
|
67 |
post.setHeader("Authorization", "Bearer " + KEY); |
|
68 |
StringEntity myEntity = new StringEntity(JSON.toJSONString(searchEntity), "UTF-8"); |
|
69 |
myEntity.setContentType("application/json;charset=UTF-8"); |
|
70 |
post.setEntity(myEntity); |
|
71 |
HttpResponse response = httpClient.execute(post); |
|
72 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
73 |
if (statusCode != HttpStatus.SC_OK) { |
|
74 |
throw new ApplicationException("获取响应失败,请重新提交-" + response.getStatusLine().getStatusCode() + "[" + response.getStatusLine().getReasonPhrase() + "]"); |
|
75 |
} else { |
|
76 |
ReverseGeocodeResponseEntity responseEntity = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"), ReverseGeocodeResponseEntity.class); |
|
77 |
if ("0".equalsIgnoreCase(responseEntity.getReturnCode())) { |
|
78 |
return responseEntity; |
|
79 |
} else if ("010004".equalsIgnoreCase(responseEntity.getReturnCode())) { |
|
80 |
//查找不到 |
|
81 |
return responseEntity; |
|
82 |
} else { |
|
83 |
throw new ApplicationException(responseEntity.getReturnDesc()); |
|
84 |
} |
|
85 |
} |
|
86 |
} catch (Exception e) { |
|
87 |
e.printStackTrace(); |
|
88 |
throw new ApplicationException("获取响应失败,请重新提交" + e.getMessage()); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
a61f53
|
93 |
public Object nearbySearch(SearchEntity searchEntity) throws Exception { |
F |
94 |
try { |
|
95 |
CoordinateEntity coordinateEntity = searchEntity.getLocation(); |
|
96 |
if (coordinateEntity == null) { |
|
97 |
throw new ApplicationException("经纬度不能为空"); |
|
98 |
} |
|
99 |
Double longitude = coordinateEntity.getLng(); |
|
100 |
Double latitude = coordinateEntity.getLat(); |
|
101 |
if (longitude != null && latitude != null) { |
|
102 |
HttpClient httpClient = HttpClients.createDefault(); |
|
103 |
HttpPost post = new HttpPost(SEARCHURL); |
|
104 |
post.setHeader("Content-Type", "application/json;charset=utf-8"); |
|
105 |
post.setHeader("Authorization", "Bearer " + KEY); |
|
106 |
StringEntity myEntity = new StringEntity(JSON.toJSONString(searchEntity), "UTF-8"); |
|
107 |
myEntity.setContentType("application/json;charset=UTF-8"); |
|
108 |
post.setEntity(myEntity); |
|
109 |
HttpResponse response = httpClient.execute(post); |
|
110 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
111 |
if (statusCode != HttpStatus.SC_OK) { |
|
112 |
throw new ApplicationException("获取响应失败,请重新提交-" + response.getStatusLine().getStatusCode() + "[" + response.getStatusLine().getReasonPhrase() + "]"); |
|
113 |
} else { |
|
114 |
ReverseGeocodeResponseEntity responseEntity = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"), ReverseGeocodeResponseEntity.class); |
|
115 |
if ("0".equalsIgnoreCase(responseEntity.getReturnCode())) { |
|
116 |
return responseEntity; |
|
117 |
} else if ("010004".equalsIgnoreCase(responseEntity.getReturnCode())) { |
|
118 |
//查找不到 |
|
119 |
return responseEntity; |
|
120 |
} else { |
|
121 |
throw new ApplicationException(responseEntity.getReturnDesc()); |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
} catch (Exception e) { |
|
126 |
e.printStackTrace(); |
|
127 |
throw new ApplicationException("获取响应失败,请重新提交" + e.getMessage()); |
|
128 |
} |
|
129 |
return null; |
|
130 |
} |
|
131 |
|
|
132 |
@Override |
03d225
|
133 |
public LocationEntity reverseGeocode(String longitude, String latitude, String ip) { |
F |
134 |
LocationEntity locationEntity = null; |
|
135 |
try { |
|
136 |
if (StringUtils.isNotBlank(longitude) && StringUtils.isNotBlank(latitude)) { |
|
137 |
HttpClient httpClient = HttpClients.createDefault(); |
|
138 |
HttpPost post = new HttpPost(GEOCODEURL); |
|
139 |
post.setHeader("Content-Type", "application/json;charset=utf-8"); |
|
140 |
post.setHeader("Authorization", "Bearer " + KEY); |
|
141 |
ReverseGeocodeRequestEntity request = new ReverseGeocodeRequestEntity(); |
|
142 |
CoordinateEntity location = new CoordinateEntity(); |
|
143 |
location.setLat(Double.parseDouble(latitude)); |
|
144 |
location.setLng(Double.parseDouble(longitude)); |
|
145 |
request.setRadius(10); |
|
146 |
request.setLocation(location); |
|
147 |
StringEntity myEntity = new StringEntity(JSON.toJSONString(request), "UTF-8"); |
|
148 |
myEntity.setContentType("application/json;charset=UTF-8"); |
|
149 |
post.setEntity(myEntity); |
|
150 |
HttpResponse response = httpClient.execute(post); |
|
151 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
152 |
if (statusCode != HttpStatus.SC_OK) { |
|
153 |
throw new ApplicationException("获取响应失败,请重新提交-" + response.getStatusLine().getStatusCode() + "[" + response.getStatusLine().getReasonPhrase() + "]"); |
|
154 |
} else { |
|
155 |
ReverseGeocodeResponseEntity responseEntity = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"), ReverseGeocodeResponseEntity.class); |
|
156 |
if ("0".equalsIgnoreCase(responseEntity.getReturnCode())) { |
|
157 |
//成功 |
|
158 |
SiteEntity siteEntity = responseEntity.getSites().get(0); |
|
159 |
AddressDetailEntity address = siteEntity.getAddress(); |
|
160 |
locationEntity = new LocationEntity(); |
|
161 |
locationEntity.setCountryName(address.getCountry());//国家 |
|
162 |
locationEntity.setProvinceName(address.getAdminArea());//省 |
|
163 |
locationEntity.setCityName(address.getCity());//市 |
|
164 |
locationEntity.setSubLocality(address.getSubLocality());//区 |
|
165 |
locationEntity.setThoroughfare(address.getThoroughfare());//街道 |
|
166 |
return locationEntity; |
|
167 |
} else { |
|
168 |
throw new ApplicationException(responseEntity.getReturnDesc()); |
|
169 |
} |
|
170 |
} |
|
171 |
} else { |
|
172 |
if (StringUtils.isNotBlank(ip)) { |
b34175
|
173 |
if ("127.0.0.1".equalsIgnoreCase(ip)) { |
F |
174 |
//本机地址不解析 |
|
175 |
return null; |
|
176 |
} |
03d225
|
177 |
locationEntity = getIPLocation(ip); |
F |
178 |
} |
|
179 |
} |
|
180 |
} catch (Exception e) { |
|
181 |
e.printStackTrace(); |
|
182 |
} |
|
183 |
return locationEntity; |
|
184 |
} |
|
185 |
} |