Android 我需要对纬度和经度进行排序。哪个离我现在的位置更近

Android 我需要对纬度和经度进行排序。哪个离我现在的位置更近,android,Android,这是我计算列表之间距离的方法。 我的代码在下面 public void calc(){ for(int i = 0; i < mSearchResultModelsToGetAppointments.size(); i++){ Location newLocation = new Location(""); newLocation.setLatitude(gpsTracker.getLatitude()); newLocation.setLongitude(gpsTr

这是我计算列表之间距离的方法。 我的代码在下面

public  void calc(){
 for(int i = 0; i < mSearchResultModelsToGetAppointments.size(); i++){
   Location newLocation = new Location("");
   newLocation.setLatitude(gpsTracker.getLatitude());
   newLocation.setLongitude(gpsTracker.getLongitude());
   if(String.valueOf(gpsTracker.getLatitude()).equalsIgnoreCase("0.0") || String.valueOf(gpsTracker.getLongitude()).equalsIgnoreCase("0.0")) {
    } else{
        Location oldLocation = new Location("");              
        oldLocation.setLatitude(mSearchResultModelUnderProcess.getLatitude());
        oldLocation.setLongitude(mSearchResultModelUnderProcess.getLongitude());   
        float distanceInMeters = newLocation.distanceTo(oldLocation)/1000;           
        if(distanceInMeters < 2820){
            getAdapter().notifyDataSetChanged();
            getAdapter().removeItem(i);
            getAdapter().notifyDataSetChanged();
        }
    }
 }
}
public void calc(){
对于(int i=0;i

这就是我现在显示位置的方式。

如果您不想使用任何外部库,我建议采用以下方式

  • 使用属性lat和lng创建您自己的自定义位置类,另外还有一个名为距离的属性,该属性测量从当前位置到该点的距离。您可以找到如何找到两点之间的距离
  • 此类应实现可比较接口并重写其比较函数。在该函数中,您可以根据距离创建自己的排序逻辑
  • 您可以找到如何使用可比较的界面,如果您还有问题,请告诉我


    Happy coding

    从问题中不清楚问题出在哪里。@Michael我正在得到一个坐标列表,我正在使用distanceto函数计算我当前位置和给定坐标之间的距离。我得到了结果,但现在我必须以这样一种方式列出它们:离我更近的一个应该排在最前面。你觉得我的答案有用吗?如果有,请投赞成票并接受它