Ben,
Try the code below. Should do the job for you.
Cheers,
David
/**
* Sort a List of LatLonPoints by their distance from a reference point.
* The contents of <CODE>latlonlist</CODE> will be sorted in place.
* _at_param latlonlist list of LatLonPoints to sort. This list will be
modified.
* _at_param llp reference LatLonPoint.
* _at_return the sorted list.
*/
public List sortLatLons(List latlonlist, LatLonPoint llp){
Collections.sort(latlonlist, new DistanceComparator(llp));
return latlonlist;
}
/**
* Compares to LatLonPoints by their distance from a reference
LatLonPoint.
*/
public class DistanceComparator implements Comparator {
LatLonPoint refPt;
/**
* Constructor for a DistanceComparator of LatLonPoints to a
reference point.
* _at_param llp reference LatLonPoint against which to compare.
*/
public DistanceComparator(LatLonPoint llp) {
refPt = llp;
}
/**
* Compares its two arguments for order. Returns a negative integer,
zero, or a
* positive integer if the first argument is less than, equal to, or
greater than
* the second.
* _at_param o1 first object to be compared.
* _at_param o2 first object to be compared.
* _at_return a negative integer, zero, or a positive integer as the
first argument is less than, equal to, or greater than the second.
*/
public int compare(Object o1, Object o2) {
//Assume the objects are always LatLonPoints
LatLonPoint llp1 = (LatLonPoint)o1;
LatLonPoint llp2 = (LatLonPoint)o2;
//Get the distances from the reference point
float dist1 = GreatCircle.spherical_distance(refPt.radlat_,
refPt.radlon_,
llp1.radlat_, llp1.radlon_);
float dist2 = GreatCircle.spherical_distance(refPt.radlat_,
refPt.radlon_,
llp2.radlat_, llp2.radlon_);
// calculate the difference
double diff = dist1-dist2;
//make sure we return an appropriate result,
//can not simply cast the difference to int
if (diff == 0.0f) return 0;
return diff < 0 ? -1 : 1;
}
}
>-----Original Message-----
>From: owner-openmap-users_at_bbn.com [mailto:owner-openmap-users_at_bbn.com]On
>Behalf Of Ben Podoll
>Sent: Wednesday, August 11, 2004 6:46 PM
>To: openmap-users_at_bbn.com
>Subject: [OpenMap Users] sort lat,lon pairs (or find distance)
>
>
>I have a list of LatLonPoint objects, and I want to sort them by distance
>from a LatLonPoint. Hence I want to make a method that could do this:
>
>public List sortLatLons(List latlonlist, LatLonPoint llp){
> //where the latlonlist is a List of LatLonPoint objects
> //sort the latlonlist in order closest to the llp
> //return the list
>}
>
>What can I use in OpenMap to accomplish this goal?
>
>Thanks,
>Ben
>
>
>--
>[To unsubscribe to this list send an email to "majdart_at_bbn.com"
>with the following text in the BODY of the message "unsubscribe
>openmap-users"]
>--
>[To unsubscribe to this list send an email to "majdart_at_bbn.com"
>with the following text in the BODY of the message "unsubscribe
>openmap-users"]
Cheers,
David
-----------------
David J. Ward
mry_yachtsman_at_hotmail.com
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
ht
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Thu Aug 12 2004 - 02:34:56 EDT