Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here goes the <a href="https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates">thread</a> that describes how you can calculate the distance between to gps coordinates. It features implementations in many languages, hopefully you will understand some of them. Then lets assume you have a function <code>calcDistance</code> that returns <code>Double</code> - the distance from the current point to hash map of your type using its longitude and latitude (btw is it requirement that you use such ugly kind of map? Can't you create a bean class for the same purpose).</p> <p>Then what you need is to declare a method of comparing two positions specified by their positions:</p> <pre><code>public class PositionComparator implements Comparator&lt;HashMap&lt;String, String&gt;&gt; { public int compare(HashMap&lt;String, String&gt; o1, HashMap&lt;String, String&gt; o2) { return calcDistance(o1).compareTo(calcDistance(o2)); } } </code></pre> <p>And finally the sorting is done like that:</p> <pre><code>Collections.sort(mylist, new PositionComparator()); </code></pre> <p>However mind that if you implement the sorting that way the distance to each element will be calculated multiple times. I recommend you to add one more field in the map that stores the distance, so that you avoid reevaluating it.</p> <p>One last note: if you decide to switch the map with a class, you can easily make this class implement the <code>Comparable</code> interface and define the comparison there.</p> <p><strong>EDIT</strong> Adding the requested example of the bean:</p> <pre><code>public class PositionBean implements Comparable { private double latitude; private double longitude; private distanceFromCurrentPosition; private String vicinity; private String name; // ...getters and setters ... public PositionBean(String latitude, String longitude, String vicinity, String name) { this.latitude = Double.valueOf(latitude); this.longitude = Double.valueOf(longitude); this.vicinity= vicinity; this.name = name; this.distanceFromCurrentPosition = calcDistance(); } public int compareTo(Object anotherPositionObj) throws ClassCastException { if (!(anotherPositionObj instanceof PositionBean)) { throw new ClassCastException("A PositionBean object expected."); } int anotherPosition = (PositionBean) anotherPositionObj; return Double.compare(this.getDistanceFromCurrentPosition(), anotherPosition.getDistanceFromCurrentPosition()); } private double calcDistance() { //...implement accordingly } } public static void main(String [] args) { List&lt;PositionBean&gt; positions = new ArrayList&lt;PositionBean&gt;(); // ... Initialize the positions Collections.sort(positions); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload