Java:与位置比较时,正确设置距离的格式
我正在开发一个Android应用程序,服务器端使用Java。当我收到用户的坐标时,我正在检索餐厅列表,我正在计算与用户的距离,并以升序方式对它们进行排序 现在,一切正常。唯一的问题是,计算的距离具有非常高的灵敏度。我要寻找的是,以这种方式显示的距离,即1.2km、200m、12.2km等,这是适当计算和附加Km或m的距离。我怎样才能做到这一点 目前的产出是:Java:与位置比较时,正确设置距离的格式,java,android,gps,location,earthdistance,Java,Android,Gps,Location,Earthdistance,我正在开发一个Android应用程序,服务器端使用Java。当我收到用户的坐标时,我正在检索餐厅列表,我正在计算与用户的距离,并以升序方式对它们进行排序 现在,一切正常。唯一的问题是,计算的距离具有非常高的灵敏度。我要寻找的是,以这种方式显示的距离,即1.2km、200m、12.2km等,这是适当计算和附加Km或m的距离。我怎样才能做到这一点 目前的产出是: Restaurant distance is 6026.203669933703 Restaurant distance is 1.024
Restaurant distance is 6026.203669933703
Restaurant distance is 1.0248447083638768
Restaurant distance is 1.0248447083638768
Restaurant distance is 1.0248447083638768
计算和排序代码:
@Override
public List<Restaurant> getNearbyRestaurants(double longitude, double latitude) {
final int R = 6371; // Radius of the earth
List<Restaurant> restaurantList = this.listRestaurants();
List<Restaurant> nearbyRestaurantList = new ArrayList<>();
for(Restaurant restaurant : restaurantList){
Double latDistance = toRad(latitude-restaurant.getLatitude());
Double lonDistance = toRad(longitude-restaurant.getLongitude());
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
Math.cos(toRad(latitude)) * Math.cos(toRad(restaurant.getLatitude())) *
Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
Double distance = R * c;
restaurant.setDistanceFromUser(distance);
if(distance < 10){
nearbyRestaurantList.add(restaurant);
}
}
if(!(nearbyRestaurantList.isEmpty())) {
Collections.sort(nearbyRestaurantList, new Comparator<Restaurant>() {
@Override
public int compare(Restaurant o1, Restaurant o2) {
if (o1.getDistanceFromUser() > o2.getDistanceFromUser()) {
return 1;
}
if (o1.getDistanceFromUser() < o2.getDistanceFromUser()) {
return -1;
}
return 0;
}
});
for(Restaurant restaurant : restaurantList){
System.out.println("Restaurant distance is "+restaurant.getDistanceFromUser());
}
return nearbyRestaurantList;
}
return null;
}
@覆盖
公共列表GetNearByRestarants(双经度、双纬度){
最终整数R=6371;//地球半径
List restaurantList=this.listRestaurants();
List nearbyRestaurantList=新建ArrayList();
适用于(餐厅:餐厅列表){
双latDistance=toRad(latitude restaurant.getLatitude());
Double lonDistance=toRad(longitude.getLongitude());
双a=Math.sin(latDistance/2)*Math.sin(latDistance/2)+
Math.cos(toRad(latitude))*Math.cos(toRad(restaurant.getLatitude())*
数学sin(lonDistance/2)*数学sin(lonDistance/2);
双c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));
双倍距离=R*c;
餐厅。设置DistancefromUser(距离);
如果(距离<10){
附近的餐馆列表。添加(餐厅);
}
}
if(!(nearbyRestaurantList.isEmpty()){
Collections.sort(nearbyrestarantlist,newcomparator(){
@凌驾
公共int比较(o1餐厅、o2餐厅){
如果(o1.getDistanceFromUser()>o2.getDistanceFromUser()){
返回1;
}
如果(o1.getDistanceFromUser()
请让我知道我错过了什么。非常感谢。:-) 如果距离小于1000米,则根据应用,使用整米的精确值,或四舍五入到下一个10米: 473.343->470m或473,具体取决于应用程序的目标 如果距离大于1km但小于100km,则使用小数点后的一位数字: 1.5公里、10.3公里、99.5公里 如果大于100公里,则为整数公里:
101km,9453km如果距离小于1000m,则根据应用情况,使用整数米的精确值,或四舍五入到下一个10米: 473.343->470m或473,具体取决于应用程序的目标 如果距离大于1km但小于100km,则使用小数点后的一位数字: 1.5公里、10.3公里、99.5公里 如果大于100公里,则为整数公里:
101km,9453km对我的答案有什么反馈吗?@AlexWien:对不起,我正在处理一些UI问题。我明天给你可以吗。很抱歉-(对我的回答有什么反馈吗?@AlexWien:对不起,我正在处理一些UI问题。如果我明天给你可以吗?我很抱歉-(对不起,耽搁了,我想你会有一些更简单的答案,比如Ed先生的答案:。你能检查一下吗。谢谢。我的答案包含3个if语句或句子,因此你应该可以轻松地将其翻译成软件。对不起耽搁了,我想你会有一些更简单的答案,比如Ed先生的答案:。能吗谢谢。我的答案包含3个if语句或句子,所以这应该很容易被你翻译成软件。