我有一个实体在数据库中有两列,就像这样:
@Column(name = "latitude", nullable = false)
private float currentPositionLatitude;
@Column(name = "longitude", nullable = false)
private float currentPositionLongitude;
我在我的数据库(PostgresQl)中有一个实体,它有多个条目,每个条目具有不同的纬度和经度。现在我想给出一个点(lat,lon)和一个半径,并显示所有靠近该点的实体。
我首先尝试使用Harversine公式在Spring Jpa中实现:
String HAVERSINE_PART = "(6371 * acos(cos(radians(:latitude)) * cos(radians(s.latitude)) *" +
" cos(radians(s.longitude) - radians(:longitude)) + sin(radians(:latitude)) * sin(radians(s.latitude))))";
@Query("SELECT currentPositionLatitude,currentPositionLongitude FROM Entity WHERE '%:HAVERSINE_PART%' < :distance ORDER BY '%:HAVERSINE_PART%' DESC")
public List<Entity> findEntiryWithLocation(
@Param("currentPositionLatitude") final float latitude,
@Param("currentPositionLongitude") final float longitude, @Param("distance") final double distance )
这个方法没有起作用,所以我尝试了:
@Query("SELECT s FROM Entity s WHERE " + HAVERSINE_PART + " < :distance ORDER BY "+ HAVERSINE_PART + " DESC")
List<Entity> findEntityWithInDistance(
@Param("latitude") double latitude,
@Param("longitude") double longitude,
@Param("distance") double distanceWithInKM );
这个也没用,所以我尝试了:
@Query(value = "SELECT *, earth_distance(ll_to_earth(:latitude, :longitude), ll_to_earth(latitude, longitude)) as distance FROM Car WHERE (earth_box(ll_to_earth(:latitude, :longitude), :radiusInMeters) @> ll_to_earth(latitude, longitude))ORDER BY distance ASC",
nativeQuery = true)
List<Entity> getPlacesNear ( @Param("latitude") float latitude,
@Param("longitude")float longitude,
@Param("radiusInMeters") float radiusInMeters);
这段代码已经编译通过,但是在我尝试在控制器中传入值时,出现了错误:jdbc error :2022 no dialect mapping。
下面是我的服务方法和控制器代码(数据类型可能已更改):
public List<Entity>showNearestEntityByDistances(float latitude, float longitude, float distance){
return EntityRepository.getPlacesNear(latitude,longitude,distance);
}
以及端点:
@GetMapping("/location")
public List<Entity>showEntityNearby(@RequestParam float latitude,@RequestParam float longitude,@RequestParam float distance){
return carService.showNearestEntitysByDistances(latitude,longitude,distance);
}
我已经查看了以下链接: https://www.postgresql.org/docs/current/earthdistance.html 和 postgresql jpa earth_distance query
但是它没有起作用。有人知道我做错了什么吗? 谢谢。