-1

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

我会通过这个函数得到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*/  
    }

我很难按距离对数组进行排序。也就是说,按最近点排序。

4

3 回答 3

2

我所做的让它工作的是,假设你有一个地方数组

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()));
            }
        });
于 2013-10-26T16:56:44.243 回答
0

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

SQlite 获取最近的位置(经纬度)

于 2013-10-26T15:52:16.330 回答
0

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

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);
       }
    }
}
于 2013-10-26T16:02:09.303 回答