-1

I have a database that has the fields, latitude and longitude.

I'll get the bd through this function,and convert to an array.

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

She also has this function to return the distance between where I am and point.

  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*/  
    }

My difficulty in this sort the array by distance. That is, sort by closest point.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Vitor Pereira
  • 11
  • 1
  • 4
  • If your issue is with sorting the data refer to this: http://stackoverflow.com/questions/19495642/java-sorting-text-file-lines/19496006#19496006 – Tyler Oct 26 '13 at 15:37

3 Answers3

3

What I did to make it work was, suppose you have an array of Places

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

and place is a class that has those fields:

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;
}
}

Then what you should do is first, go over all the array to find the distance of each location from your location:

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);
}

Finally, sort this array by 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()));
            }
        });
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
0

You will want to check out this answer which demonstrates how to construct the order by statement to sort by distance.

SQlite Getting nearest locations (with latitude and longitude)

Community
  • 1
  • 1
skynet
  • 9,898
  • 5
  • 43
  • 52
0

This is how I would probably do it...

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);
       }
    }
}
pscuderi
  • 1,554
  • 12
  • 14