1.距离计算工具
public class LocationUtils_Distant {
private static final double EARTH_RADIUS = 6378.137;
private double rad(double d) {
return d * Math.PI / 180.0;
}
public double getDistance(double lat1, double lng1, double lat2, double lng2) {
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000d) / 10000d;
s = s * 1000;
return s;
}
}
代码传参实现过程
Double gps_x1 = Double.valueOf((String) pd.get("gps_x"));
Double gps_y1 = Double.valueOf((String) pd.get("gps_y"));
List<PageData> orderPosition = service.findByOrderPosition(pd);
if (orderPosition == null) {
map.put("result", "null");
map.put("status", "-1");
} else {
for (PageData pageData : orderPosition) {
double gps_x = Double.valueOf((String) pageData.get("gps_x"));
double gps_y = Double.valueOf((String) pageData.get("gps_y"));
LocationUtils_Distant locationUtils_Distant = new LocationUtils_Distant();
double distant_meter1 = locationUtils_Distant.getDistance(gps_y1, gps_x1, gps_y, gps_x);
String distant_meter = String.valueOf(distant_meter1 / 1000);
pageData.put("distant_meter", distant_meter);
}
map.put("result", "success");
map.put("data", orderPosition);
}
findByOrderPosition
public List<PageData> findByOrderPosition(PageData pd) throws Exception {
Double gps_x = Double.valueOf((String) pd.get("gps_x"));
Double gps_y = Double.valueOf((String) pd.get("gps_y"));
Double distant = Double.valueOf((String) pd.get("distant"));
int orderType = Integer.valueOf((String) pd.get("orderType"));
String user_id=pd.getString("user_id");
SpatialContext geo = SpatialContext.GEO;
Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(geo.makePoint(gps_y, gps_x),
distant * DistanceUtils.KM_TO_DEG, geo, null);
Double Max_gps_y = rectangle.getMaxX();
Double Min_gps_y = rectangle.getMinX();
Double Max_gps_x = rectangle.getMaxY();
Double Min_gps_x = rectangle.getMinY();
Map<String, Object> map_1 = new HashMap<String, Object>();
map_1.put("Max_gps_x", Max_gps_x);
map_1.put("Min_gps_x", Min_gps_x);
map_1.put("Max_gps_y", Max_gps_y);
map_1.put("Min_gps_y", Min_gps_y);
map_1.put("orderType", orderType);
map_1.put("gps_x", gps_x);
map_1.put("gps_y", gps_y);
map_1.put("user_id", user_id);
return (List<PageData>) dao.findForList("AppRobsMapper.findByOrderPosition", map_1);
}
2.Mysql语句
时间格式化:DATE_FORMAT(o.start_time,’%Y/%m/%d %H:%i:%s’) start_time
判断查询字段值为空时赋值-1
IFNULL(c.name, ‘-1’) name
IFNULL(c.name
, ‘崔大小’)
替换方法
start_time = start_time.replaceAll("T", " ").replaceAll("Z", "").replaceAll("-", "/");
end_time = end_time.replaceAll("T", " ").replaceAll("Z", "").replaceAll("-", "/");
生成id
String id = this.get32UUID();
public String get32UUID(){
return UuidUtil.get32UUID();
}
时间格式
SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddHHmmss");
时间相减转换成天数
TO_DAYS(NOW()) - TO_DAYS(d.create_time) >= ad.days