根据给定地点的经纬度(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
}