我先说明一下定定义,拉运地:地图中的一个点包含经度和维度,也是“起点”,运往地:地图中的一个点包含经度和维度,也是“终点”。我的数据库里存放着很多拉运地和运往地的坐标点。现在,我想要一个java语言编写的代码,我可以传入拉运地的偏差范围和运往地的偏差范围,然后从数据库中给我返回符合条件的坐标信息。注意:1. 我用的持久化框架是mybaties plus。2. 我传入的偏差范围是一个单位为"千米"的数。3. 我要的代码类型为工具类
时间: 2023-03-30 07:02:22 浏览: 93
好的,根据您的需求,我可以为您提供以下的Java工具类代码:
```
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
public class CoordinateUtils {
/**
* 根据给定的拉运地偏差范围和运往地偏差范围,从数据库中查询符合条件的坐标信息
*
* @param baseMapper 数据库操作对象
* @param startLatitude 拉运地纬度
* @param startLongitude 拉运地经度
* @param startRange 拉运地偏差范围(单位:千米)
* @param endLatitude 运往地纬度
* @param endLongitude 运往地经度
* @param endRange 运往地偏差范围(单位:千米)
* @return 符合条件的坐标信息列表
*/
public static <T extends CoordinateEntity> List<T> queryCoordinatesInRange(BaseMapper<T> baseMapper,
double startLatitude,
double startLongitude,
double startRange,
double endLatitude,
double endLongitude,
double endRange) {
// 计算拉运地和运往地的经纬度范围
double startLatitudeMin = startLatitude - startRange / 111.;
double startLatitudeMax = startLatitude + startRange / 111.;
double startLongitudeMin = startLongitude - startRange / (111. * Math.cos(startLatitude));
double startLongitudeMax = startLongitude + startRange / (111. * Math.cos(startLatitude));
double endLatitudeMin = endLatitude - endRange / 111.;
double endLatitudeMax = endLatitude + endRange / 111.;
double endLongitudeMin = endLongitude - endRange / (111. * Math.cos(endLatitude));
double endLongitudeMax = endLongitude + endRange / (111. * Math.cos(endLatitude));
// 构造查询条件
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper.between("start_latitude", startLatitudeMin, startLatitudeMax)
.between("start_longitude", startLongitudeMin, startLongitudeMax)
.between("end_latitude", endLatitudeMin, endLatitudeMax)
.between("end_longitude", endLongitudeMin, endLongitudeMax);
// 执行查询
return baseMapper.selectList(queryWrapper);
}
/**
* 坐标实体类,需要实现经纬度属性的get方法
*/
public interface CoordinateEntity {
double getStartLatitude();
double getStartLongitude();
double getEndLatitude();
double getEndLongitude();
}
}
```
您可以将此代码保存为一个Java文件,并将其放入您的项目中。使用时,您需要将`CoordinateEntity`接口替换为您自己的坐标实体类,并传入相应的参数调用`queryCoordinatesInRange`方法即可。