经纬度计算距离在HQL中应用实例
2010-07-23 14:17
369 查看
根据给定地点的经纬度(longitude,latitude),查询给定范围(distance)内的对象列表,由近及远。
java计算公式:
def lat1 = 32.361403
def lat2 = 40.245992
def lng1 = 118.78418
def lng2 = 116.477051
double EARTH_RADIUS = 6378.137; //地球半径
double radLat1 = lat1* Math.PI / 180.0
double radLat2 = lat2* Math.PI / 180.0
double a = radLat1 - radLat2;
double b = lng1* Math.PI / 180.0 - lng2* Math.PI / 180.0
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 * 10000) / 10000;
return s
实例:
List<Place> getClosestPlacesByLatitudeAndLongitude(Double distance, String pageId, Double latitude, Double longitude, int offset, int length) {
def placeList = []
if (pageId) {
def page = accountService.getContentProviderForPage(pageId)
if (!page?.rewardsEnabled) {
return new ArrayList()
}
placeList = Place.executeQuery("from Place as p where p.contentProvider =:page and (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude)
* 3.1415926 / 360), 2)))*12756274) < :distance Order by (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude) * 3.1415926 / 360), 2)))*12756274)
asc name asc", [distance:distance, page: page, latitude: latitude, longitude: longitude, max: length, offset: offset])
} else {
placeList = Place.executeQuery("from Place as p where (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude) * 3.1415926 / 360), 2)))*12756274)
< :distance Order by (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude) * 3.1415926 / 360), 2)))*12756274) asc name asc", [distance:distance,
latitude: latitude, longitude: longitude, max: length, offset: offset])
}
return placeList
}
java计算公式:
def lat1 = 32.361403
def lat2 = 40.245992
def lng1 = 118.78418
def lng2 = 116.477051
double EARTH_RADIUS = 6378.137; //地球半径
double radLat1 = lat1* Math.PI / 180.0
double radLat2 = lat2* Math.PI / 180.0
double a = radLat1 - radLat2;
double b = lng1* Math.PI / 180.0 - lng2* Math.PI / 180.0
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 * 10000) / 10000;
return s
实例:
List<Place> getClosestPlacesByLatitudeAndLongitude(Double distance, String pageId, Double latitude, Double longitude, int offset, int length) {
def placeList = []
if (pageId) {
def page = accountService.getContentProviderForPage(pageId)
if (!page?.rewardsEnabled) {
return new ArrayList()
}
placeList = Place.executeQuery("from Place as p where p.contentProvider =:page and (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude)
* 3.1415926 / 360), 2)))*12756274) < :distance Order by (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude) * 3.1415926 / 360), 2)))*12756274)
asc name asc", [distance:distance, page: page, latitude: latitude, longitude: longitude, max: length, offset: offset])
} else {
placeList = Place.executeQuery("from Place as p where (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude) * 3.1415926 / 360), 2)))*12756274)
< :distance Order by (asin(sqrt(pow(sin((p.latitude - :latitude) * 3.1415926 / 360), 2) + cos(p.latitude * 3.1415926 / 180) * cos(:latitude * 3.1415926 / 180) * pow(sin((p.longitude - :longitude) * 3.1415926 / 360), 2)))*12756274) asc name asc", [distance:distance,
latitude: latitude, longitude: longitude, max: length, offset: offset])
}
return placeList
}
相关文章推荐
- 根据两点经纬度计算距离
- [转]根据两点经纬度计算距离
- (iPhone/iPad开发)由经纬度计算距离
- 求通过经纬度计算地球表面任意两点间距离的公式
- 根据两点经纬度计算距离
- 根据两点经纬度计算距离
- 计算两个经纬度之间的距离
- Android-根据两点间经纬度坐标,计算两点间距离
- java根据经纬度坐标计算两点的距离算法
- PHP实现经纬度计算距离
- PHP根据经纬度计算两地之间的距离
- 计算两个经纬度之间的距离
- java根据经纬度计算距离
- mysql 通过经纬度计算距离
- 根据两点经纬度计算距离
- 计算两个经纬度的距离(米)
- PostgreSQL 遗传学应用 - 矩阵相似距离计算 (欧式距离,...XX距离)
- Java、SQLServer 根据经纬度计算距离
- SQL语句实现根据经纬度计算距离并排序分页
- 根据经纬度坐标计算两点间几何距离