2012-02-26 59 views
2

我有當前位置緯度&經度與我。我從Google地方API Web服務獲取用戶附近ATM的列表,並將我的數據存儲在Arraylist中,如下所示。如何在Android中對HashMap(ArrayList內)進行排序?

代碼段

double currentLat = xxx; 
double currentLang = xxx; 

按鈕點擊我叫谷歌地方的Web服務。

public void buttonClicked(View v) { 
    ParseXMLData xmlData = new ParseXMLData(); 
    url = baseUrl + "location=" + latitude + "," + longitude + "&" + "radius=" + search_Area_Radius + "&" + "name=" + nameofplace + "&" + "sensor=true" + "&" + "key=" + API_KEY; 
    xmlData.execute(""); 
} 

存儲內部ArrayList中的結果....

for (int i = 0; i < nodes.getLength(); i++) { 
     HashMap<String, String> map = new HashMap<String, String>(); 
     Element e = (Element)nodes.item(i); 
     map.put("Name", XMLFunctions.getValue(e, "name")); 
     map.put("Vicinity", XMLFunctions.getValue(e, "vicinity")); 
     map.put("Latitude", XMLFunctions.getValue(e, "lat")); 
     map.put("Longitude", XMLFunctions.getValue(e, "lng")); 

     loc = new Location(""); 
     loc.setLatitude(Double.parseDouble(XMLFunctions.getValue(e, "lat"))); 
     loc.setLongitude(Double.parseDouble(XMLFunctions.getValue(e, "lng"))); 

獲得的B/W 2點之間的距離在這裏弗羅姆。

 distance = currentLoc.distanceTo(loc); 
     map.put("Distance",String.valueOf(distance)); 
     mylist.add(map); 
     loc = null; 
     distance = 0.0; 
    } 

實現的比較和排序ArrayList的

public class PositionComparator implements Comparator<HashMap<String, String>> { 

    public PositionComparator() { 
     // TODO Auto-generated constructor stub 
    } 


    public int compare(HashMap<String, String> o1, HashMap<String, String> o2) { 
     /*System.out.println(o1.get("O1 Distance :- ")); 
     System.out.println(o2.get("O2 Distance :- ")); 
     System.out.println(o1.get("Distance").compareTo(o2.get("Distance"))); 
     */ 
     return o1.get("Distance").compareTo(o2.get("Distance")); 
    } 
} 

現在發生什麼事是我有店內的數組列表進行排序,但問題是基於compareTo方法排序數據的數據僅限第一個字符。假設我的數據是2.12345和11.32545那麼它將取代爲2-1 = 1.

如何解決這個問題?

如果有人有任何建議/提示,請在此幫助我。

感謝

+0

是不是太多的工作比較ArrayList中的結果,並將它們存儲在你想看到它們的順序數組?一旦你運行了你的查詢並得到了你的結果,你就知道該數組應該有多少個元素。 – 2012-02-26 09:18:45

回答

3

這裏去的thread描述如何計算之間GPS座標的距離。它具有許多語言的實現,希望你能理解其中的一些。然後讓我們假設你有一個函數calcDistance返回Double - 使用它的經度和緯度從當前點到你類型的散列圖的距離(btw是否需要使用這種醜陋的地圖?你不能創建一個bean班級爲同一目的)。

那麼你需要的是申報比較由它們的位置指定的兩個位置的方法:

public class PositionComparator implements Comparator<HashMap<String, String>> { 
    public int compare(HashMap<String, String> o1, HashMap<String, String> o2) { 
     return calcDistance(o1).compareTo(calcDistance(o2)); 
    } 
} 

最後的排序是這樣完成的:

Collections.sort(mylist, new PositionComparator()); 

但是記住,如果你以這種方式實施排序,每個元素的距離將被多次計算。我建議您在地圖上添加一個存儲距離的字段,以避免重新評估它。

最後一個注意事項:如果您決定使用班級切換地圖,您可以輕鬆地讓該班級實施Comparable界面並在那裏定義比較。

編輯添加bean的請求例如:

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<PositionBean> positions = new ArrayList<PositionBean>(); 
    // ... Initialize the positions 
    Collections.sort(positions); 
} 
+0

非常感謝您的快速響應。假設我創建Bean(名稱,鄰近度,緯度,土地,距離與getter&setter方法)。如何在那裏使用Comparable接口?你能給我一些總體的想法嗎? – Scorpion 2012-02-27 03:46:19

+1

當然。我已經添加了這個例子。不過,請原諒我,如果我犯了小的打字錯誤,因爲我直接填寫文本編輯器。如果有任何問題,請回復。 – 2012-02-27 12:56:02

+0

感謝buddy它的完成..... – Scorpion 2012-03-07 06:42:17

相關問題