【问题标题】:Sorting array by the distance [duplicate]按距离排序数组[重复]
【发布时间】:2013-11-05 16:08:27
【问题描述】:

我有一个包含纬度和经度字段的数据库。

我会通过这个函数得到bd,并转换成一个数组。

ArrayList<PontoEntity> array = new ArrayList<PontoEntity>();       
Cursor c = com.vianaturismo.db.DBMain.getAll(getApplicationContext(), DALPonto.TABLE_NAME, DALPonto.columns);
array = DALPonto.converte(c);

她还有这个功能可以返回我所在的位置和点之间的距离。

  public double getDistancia(double latitude, double longitude, double latitudePto, double longitudePto){  
            double dlon, dlat, a, distancia;  
            dlon = longitudePto - longitude;  
            dlat = latitudePto - latitude;  
            a = Math.pow(Math.sin(dlat/2),2) + Math.cos(latitude) * Math.cos(latitudePto) * Math.pow(Math.sin(dlon/2),2);  
            distancia = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));  
            return 6378140 * distancia; /* 6378140 is the radius of the Earth in meters*/  
    }

我在按距离对数组进行排序时遇到的困难。即按最近点排序。

【问题讨论】:

标签: java android arrays google-maps


【解决方案1】:

我所做的是,假设你有一个 Places 数组

private List<Places> placesList = new ArrayList<Places>();

place 是一个具有这些字段的类:

public class Place
{
private String placepName;
private String placeLat;
private String placeLng;
private float placeDistance;

public String getPlacepName() {
    return placepName;
}
public void setPlacepName(String placepName) {
    this.placepName = placepName;
}
public String getPlaceLat() {
    return placeLat;
}
public void setPlaceLat(String placeLat) {
    this.placeLat = placeLat;
}
public String getPlaceLng() {
    return placeLng;
}
public void setPlaceLng(String placeLng) {
    this.placeLng = placeLng;
}
public float getPlaceDistance() {
    return placeDistance;
}
public void setPlaceDistance(float placeDistance) {
    this.placeDistance = placeDistance;
}
}

那么你应该做的是首先,遍历所有数组以找到每个位置与你所在位置的距离:

for ( Place tempPlace: placeList)
{
    tempLocation = new Location("");    
    tempLocation.setLatitude(Double.parseDouble(tempPlace.getPlaceLat()));
    tempLocation.setLongitude(Double.parseDouble(tempPlace.getPlaceLng()));
    float distance = currentUserLocation.distanceTo(tempLocation);
    distance = distance/1000;
    tempPlace.setPlaceDistance(distance);
}

最后,按距离对这个数组进行排序:

Collections.sort(placeList, new Comparator<Place>() {
            @Override
            public int compare(Place c1, Place c2) {
                return new Float(c1.getPlaceDistance()).compareTo(new Float(c2.getPlaceDistance()));
            }
        });

【讨论】:

    【解决方案2】:

    这就是我可能会这样做的方式......

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.ListIterator;
    
    public class SortByDistance{
        public static void main(String[] args) {
            // TODO: initialize these
            List<Location> locations = new ArrayList<>();
            final Location myLocation = null;
    
            sort(locations, new ToComparable<Location, Double>() {
                @Override
                public Double toComparable(Location location) {
                    return Location.distance(location, myLocation);
                }
            });
    
            for (Location location : locations)
                System.out.println(location);
        }
    
        protected static class Location {
            private final double latitude, longitude;
    
            public Location(double latitude, double longitude) {
                this.latitude = latitude;
                this.longitude = longitude;
            }
    
            public Double getLatitude() {
                return latitude;
            }
    
            public Double getLongitude() {
                return longitude;
            }
    
            @Override
            public String toString() {
                return String.format("[latitude = %f, longitude = %f]", latitude, longitude);
            }
    
            public static double distance(Location location1, Location location2) {
                // TODO: return the distance between location1 and location2
                return 0;
            }
        }
    
        public interface ToComparable<T, C extends Comparable<? super C>> {
             C toComparable(T t);
        }
    
        public static <T, C extends Comparable<? super C>> void sort(List<T> list, ToComparable<T, C> function) {
           class Pair implements Comparable<Pair> {
              final T original;
              final C comparable;
    
              Pair(T original, C comparable) {
                 this.original = original;
                 this.comparable = comparable;
              }
    
              @Override
              public int compareTo(Pair pair) {
                  return comparable == null ? 
                    pair.comparable == null ? 0 : -1 :
                    pair.comparable == null ? 1 : comparable.compareTo(pair.comparable);
              }
           }
    
           List<Pair> pairs = new ArrayList<>(list.size());
           for (T original : list)
              pairs.add(new Pair(original, function.toComparable(original)));
    
           Collections.sort(pairs);
    
           ListIterator<T> iter = list.listIterator();
           for (Pair pair : pairs) {
              iter.next();
              iter.set(pair.original);
           }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您将需要查看此答案,该答案演示了如何构造 order by 语句以按距离排序。

      SQlite Getting nearest locations (with latitude and longitude)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-11
        • 1970-01-01
        • 2018-02-02
        • 2015-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多