实现经纬度转换为三级区域 by 陈风帆

This commit is contained in:
Mickel
2020-08-24 11:58:53 +08:00
parent e62446590d
commit e985087092

View File

@@ -1,7 +1,19 @@
package com.pgmmers.radar.service.impl.engine.Plugin;
import com.alibaba.fastjson.JSONObject;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.service.engine.vo.Location;
import com.pgmmers.radar.vo.model.PreItemVO;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
@@ -9,6 +21,7 @@ import java.util.Map;
*/
public class GPS2LOCATION implements PluginServiceV2 {
private static final Logger logger = LoggerFactory.getLogger(GPS2LOCATION.class);
@Override
public Integer key() {
return 2;
@@ -33,6 +46,42 @@ public class GPS2LOCATION implements PluginServiceV2 {
public Object handle(PreItemVO item, Map<String, Object> jsonInfo,
String[] sourceField) {
// TODO 可以参考 http://jwd.funnyapi.com/#/index , 最好是本地库。
return null;
Location location = new Location();
String latitude = String.valueOf(jsonInfo.get(sourceField[0]));//纬度
String longitude = String.valueOf(jsonInfo.get(sourceField[1]));//经度
if ("".equals(latitude) || "".equals(longitude)) {
return null;
}
try {
String url ="http://106.75.35.67:60000/gis?auth_user=freevip&latitude="+latitude+"&longitude="+longitude;
//创建httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建GET对象
HttpGet httpget = new HttpGet(url);
//执行请求
CloseableHttpResponse response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==200) {
HttpEntity entity = response.getEntity();
//所需内容都在entity里面这里可以对数据操作
String detail = EntityUtils.toString(entity,"UTF-8");
JSONObject parseObject = JSONObject.parseObject(detail);
//String转map
String data = parseObject.getString("data");
Map stringToMap = JSONObject.parseObject(data);
location.setCountry(String.valueOf(stringToMap.get("zh0")));
location.setRegion(String.valueOf(stringToMap.get("zh3")));
location.setProvince(String.valueOf(stringToMap.get("zh1")));
location.setCity(String.valueOf(stringToMap.get("zh2")));
}
response.close();
httpclient.close();
} catch (Exception e) {
logger.error("GPS2LOCATION error", e);
}
return location;
}
}