我有一个包含字段,纬度和经度的数据库。
我将通过此函数获取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*/
}
我的这种阵列的距离很难。也就是说,按最近点排序。
答案 0 :(得分:1)
我做的工作是,假设你有一系列的地方
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()));
}
});
答案 1 :(得分:0)
您需要查看此答案,该答案演示了如何构建order by
语句以按距离排序。
SQlite Getting nearest locations (with latitude and longitude)
答案 2 :(得分: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);
}
}
}