public Map<String, Object> getPharmacyByMe(double longitude, double latitude) {
Map<String, Object> mapTotal = new HashMap<>();
List<Markers> list = new ArrayList<>();
try {
String log=""+longitude;
String lat1=""+latitude;
String addCity = JingWeiUtil.getAddCity(lat1, log);
List<Pharmacy> sList = pharmacyMapper.selectList(new EntityWrapper<Pharmacy>()(判断逻辑) );
List<Map<String, Object>> mapList = new ArrayList<>();
for (Pharmacy sc : sList) {
Double lat = Double.parseDouble(sc.getLatitude());
Double lng = Double.parseDouble(sc.getLongitude());
Double ju = MapUtil.getDistance(lat, lng, latitude, longitude);
Map<String, Object> map = new HashMap<>();
map.put("id", sc.getPharmacyId());
map.put("name", sc.getPharmacyName());
map.put("address", sc.getAddress());
map.put("ju", ju);
map.put("mobile", sc.getPharmacyPhone());
map.put("dan", "");
mapList.add(map);
Callout out = new Callout();
out.setContent(sc.getPharmacyName());
Markers ma = new Markers();
ma.setCallout(out);
ma.setLatitude(lat);
ma.setLongitude(lng);
ma.setTitle(sc.getPharmacyName());
ma.setId(sc.getPharmacyId());
list.add(ma);
}
Collections.sort(mapList, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
int map1value = (int) Double.parseDouble(o1.get("ju").toString()) * 100;
int map2value = (int) Double.parseDouble(o2.get("ju").toString()) * 100;
return map1value - map2value;
}
});
List<Map<String, Object>> mapList1 = new ArrayList<>();
for(Map<String, Object> ma:mapList) {
double ju=Double.parseDouble(ma.get("ju").toString());
DecimalFormat df = new DecimalFormat("######0.00");
DecimalFormat df1 = new DecimalFormat("######0");
String juli="";
String dan="";
if(ju>1000) {
ju=ju/1000;
juli=df.format(ju);
dan="km";
}else {
juli=df1.format(ju);
dan="m";
}
ma.put("ju", juli);
ma.put("dan", dan);
mapList1.add(ma);
}
mapTotal.put("mapList", mapList1);
mapTotal.put("marList", list);
}
本文介绍如何使用MyBatisPlus结合地理坐标(经纬度)来计算两个位置之间的距离,适用于需要根据地理位置进行筛选或排序的应用场景。

1629

被折叠的 条评论
为什么被折叠?



