经纬度查周边
This commit is contained in:
+106
-1
@@ -19,6 +19,7 @@ import com.fangjia.service.datastore.DistrictService;
|
|||||||
import com.fangjia.service.mongo.fangjia.CbmRegionService;
|
import com.fangjia.service.mongo.fangjia.CbmRegionService;
|
||||||
import net.sf.json.JSONObject;
|
import net.sf.json.JSONObject;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -31,6 +32,8 @@ public class PoiController {
|
|||||||
private AddressParserService addressParserService;
|
private AddressParserService addressParserService;
|
||||||
private DistrictService districtService;
|
private DistrictService districtService;
|
||||||
private static final String[] PROPERTY_SEARCH_API_FIELDS = {"address", "city", "region", "name", "addrAlias"};
|
private static final String[] PROPERTY_SEARCH_API_FIELDS = {"address", "city", "region", "name", "addrAlias"};
|
||||||
|
private static final String ES_USER = "developer";
|
||||||
|
private static final String ES_PASSWORD = "5e8edd851d2fdfbd7415232c67367cc3";
|
||||||
|
|
||||||
public Object getAntiFraudQuestionAction(@Valid AntiFraudQuestionModel model) {
|
public Object getAntiFraudQuestionAction(@Valid AntiFraudQuestionModel model) {
|
||||||
RestfulModelAndView mav = new RestfulModelAndView();
|
RestfulModelAndView mav = new RestfulModelAndView();
|
||||||
@@ -221,7 +224,7 @@ public class PoiController {
|
|||||||
public Object searchAction(AddressModel model) {
|
public Object searchAction(AddressModel model) {
|
||||||
|
|
||||||
RestfulModelAndView mav = new RestfulModelAndView();
|
RestfulModelAndView mav = new RestfulModelAndView();
|
||||||
if (StringUtils.isBlank(model.getLocation()) || StringUtils.isBlank(model.getCity())) {
|
if (StringUtils.isBlank(model.getLocation()) || StringUtils.isBlank(model.getCity())) {
|
||||||
mav.setModel(BasicModel.buildBadRequestModel("城市和经纬度为必填项"));
|
mav.setModel(BasicModel.buildBadRequestModel("城市和经纬度为必填项"));
|
||||||
return mav;
|
return mav;
|
||||||
}
|
}
|
||||||
@@ -268,6 +271,108 @@ public class PoiController {
|
|||||||
return mav;
|
return mav;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object facilitiesAction(AddressModel model) {
|
||||||
|
RestfulModelAndView mav = new RestfulModelAndView();
|
||||||
|
String city = model.getCity();
|
||||||
|
String lngStr = model.getLng();
|
||||||
|
String latStr = model.getLat();
|
||||||
|
String numberPattern = "-?\\d+(\\.\\d+)?"; // 匹配整数或浮点数
|
||||||
|
if (!lngStr.matches(numberPattern) || !latStr.matches(numberPattern)) {
|
||||||
|
mav.setModel(BasicModel.buildBadRequestModel("location(经纬度)格式错误"));
|
||||||
|
return mav;
|
||||||
|
}
|
||||||
|
double lng = Double.parseDouble(lngStr);
|
||||||
|
double lat = Double.parseDouble(latStr);
|
||||||
|
Map<String, List<String>> result = new HashMap<String, List<String>>();
|
||||||
|
List<String> bankSmallType = Arrays.asList("银行", "中国人民银行", "国家开发银行", "中国进出口银行");
|
||||||
|
List<String> busSmallType = Collections.singletonList("公交车站相关");
|
||||||
|
List<String> hospitalSmallType = Arrays.asList("综合医院", "三级甲等医院", "专科医院");
|
||||||
|
List<String> schoolSmallType = Arrays.asList("学校", "高等院校", "中学", "小学", "幼儿园");
|
||||||
|
List<String> subwaySmallType = Collections.singletonList("地铁站");
|
||||||
|
List<String> supermarketSmallType = Arrays.asList("商场", "购物中心", "普通商场");
|
||||||
|
|
||||||
|
result.put("bank", findAroundByLocation(city, lng, lat, bankSmallType, 1000, true));
|
||||||
|
result.put("bus", findAroundByLocation(city, lng, lat, busSmallType, 1000, false));
|
||||||
|
result.put("hospital", findAroundByLocation(city, lng, lat, hospitalSmallType, 1000, true));
|
||||||
|
result.put("school", findAroundByLocation(city, lng, lat, schoolSmallType, 1000, true));
|
||||||
|
result.put("subway", findAroundByLocation(city, lng, lat, subwaySmallType, 1000, false));
|
||||||
|
result.put("supermarket", findAroundByLocation(city, lng, lat, supermarketSmallType, 1000, true));
|
||||||
|
mav.setModel(BasicModel.buildOKModel().setResult(result));
|
||||||
|
return mav;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> findAroundByLocation(String city, double lng, double lat, List<String> smallTypes, int distance, boolean excludeStatus) {
|
||||||
|
List<String> poiNames = new ArrayList<String>();
|
||||||
|
JSONObject reqBody = new JSONObject();
|
||||||
|
JSONObject queryJson = new JSONObject();
|
||||||
|
List<JSONObject> jsonList = new ArrayList<JSONObject>();
|
||||||
|
List<JSONObject> mustNotList = new ArrayList<JSONObject>();
|
||||||
|
|
||||||
|
if (excludeStatus) {
|
||||||
|
JSONObject statusParam = new JSONObject();
|
||||||
|
statusParam.put("status", 1);
|
||||||
|
JSONObject statusTerm = new JSONObject();
|
||||||
|
statusTerm.put("term", statusParam);
|
||||||
|
mustNotList.add(statusTerm);
|
||||||
|
}
|
||||||
|
JSONObject mainTypeParam = new JSONObject();
|
||||||
|
mainTypeParam.put("main_type", 0);
|
||||||
|
JSONObject mainTypeTerm = new JSONObject();
|
||||||
|
mainTypeTerm.put("term", mainTypeParam);
|
||||||
|
mustNotList.add(mainTypeTerm);
|
||||||
|
|
||||||
|
JSONObject cityParam = new JSONObject();
|
||||||
|
cityParam.put("city", city);
|
||||||
|
JSONObject cityTerm = new JSONObject();
|
||||||
|
cityTerm.put("term", cityParam);
|
||||||
|
jsonList.add(cityTerm);
|
||||||
|
|
||||||
|
JSONObject smallTypeParam = new JSONObject();
|
||||||
|
smallTypeParam.put("small_type", smallTypes);
|
||||||
|
JSONObject smallTypeTerm = new JSONObject();
|
||||||
|
smallTypeTerm.put("terms", smallTypeParam);
|
||||||
|
jsonList.add(smallTypeTerm);
|
||||||
|
|
||||||
|
JSONObject geoDistance = new JSONObject();
|
||||||
|
geoDistance.put("distance", distance + "m");
|
||||||
|
geoDistance.put("location", Arrays.asList(lng, lat));
|
||||||
|
JSONObject geoQuery = new JSONObject();
|
||||||
|
geoQuery.put("geo_distance", geoDistance);
|
||||||
|
jsonList.add(geoQuery);
|
||||||
|
|
||||||
|
queryJson.put("must", jsonList);
|
||||||
|
if (!mustNotList.isEmpty()) {
|
||||||
|
queryJson.put("must_not", mustNotList);
|
||||||
|
}
|
||||||
|
JSONObject boolQuery = new JSONObject();
|
||||||
|
boolQuery.put("bool", queryJson);
|
||||||
|
reqBody.put("query", boolQuery);
|
||||||
|
|
||||||
|
Base64 token = new Base64();
|
||||||
|
String authEncoding = token.encodeAsString((ES_USER + ":" + ES_PASSWORD).getBytes());
|
||||||
|
HttpRequest request = new HttpRequest();
|
||||||
|
Map<String, String> headers = new HashMap<String, String>();
|
||||||
|
headers.put("Content-Type", "application/json");
|
||||||
|
headers.put("Authorization", "Basic " + authEncoding);
|
||||||
|
|
||||||
|
try {
|
||||||
|
String response = request.postWithBody("http://192.168.0.89:9200/gaode_map_poi/_search?rest_total_hits_as_int=true", reqBody, headers, "UTF-8");
|
||||||
|
JSONObject json = JSONObject.fromObject(response);
|
||||||
|
List<Map<String, Object>> hits = (List<Map<String, Object>>) json.getJSONObject("hits").get("hits");
|
||||||
|
|
||||||
|
for (Map<String, Object> hit : hits) {
|
||||||
|
Map<String, Object> source = (Map<String, Object>) hit.get("_source");
|
||||||
|
if (source.containsKey("name")) {
|
||||||
|
poiNames.add(source.get("name").toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return poiNames;
|
||||||
|
}
|
||||||
|
|
||||||
public void setAmapPoiService(AmapPoiService amapPoiService) {
|
public void setAmapPoiService(AmapPoiService amapPoiService) {
|
||||||
this.amapPoiService = amapPoiService;
|
this.amapPoiService = amapPoiService;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user