Advertisement
rachmadi

Sorting distance

Jan 8th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. JSONArray tambalban = null;
  2. ArrayList<HashMap<String, String>> listMotor = new ArrayList<HashMap<String,String>>();
  3.  
  4. @Override
  5. public void onCreate(Bundle savedInstanceState) {
  6.     super.onCreate(savedInstanceState);
  7.     new GetJSON().execute();
  8. }
  9.  
  10.  public class GetJSON extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>>{
  11.  
  12. @Override
  13. protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
  14.     // TODO Auto-generated method stub
  15.     JSONParser jParser = new JSONParser();
  16.     JSONObject json = jParser.getJSONFromURL(url);
  17.     try {
  18.         tambalban = json.getJSONArray(TAG_MOTOR);
  19.  
  20.         for (int i = 0; i < tambalban.length(); i++){
  21.             JSONObject mtr = tambalban.getJSONObject(i);
  22.  
  23.             String id_tb = mtr.getString(TAG_ID);
  24.             String name_tb = mtr.getString(TAG_NAME);
  25.             String address_tb = mtr.getString(TAG_ADDRESS);
  26.             String telp_tb = mtr.getString(TAG_TELP);
  27.             String lat = mtr.getString(TAG_LAT);
  28.             String lng = mtr.getString(TAG_LNG);
  29.  
  30.             double lat_tujuan = Double.parseDouble(lat);
  31.             double lng_tujuan = Double.parseDouble(lng);
  32.             double lat_asal = -7.781083333;
  33.             double lng_asal = 110.4824833;
  34.  
  35.             double distance = hitungJarak (lat_asal, lng_asal, lat_tujuan, lng_tujuan);
  36.             distance = RoundDecimal(distance, 2);
  37.             String jarak = Double.toString(distance)+ " km";
  38.  
  39.             HashMap<String, String> a = new HashMap<String, String>();
  40.             a.put(TAG_ID, id_tb);
  41.             a.put(TAG_NAME, name_tb);
  42.             a.put(TAG_ADDRESS, address_tb);
  43.             a.put(TAG_TELP, telp_tb);
  44.             a.put(TAG_LAT, lat);
  45.             a.put(TAG_LNG, lng);
  46.             a.put(TAG_DISTANCE, distance);
  47.  
  48.             listMotor.add(a);
  49.  
  50. **Sorting Code**                
  51. Collections.sort(listMotor, new Comparator<HashMap<String, String>>() {
  52.                 @Override
  53.                 public int compare(HashMap<String, String> o1,
  54.                         HashMap<String, String> o2) {
  55.  
  56.                     return Double.compare(o1.get(Double.parseDouble(TAG_DISTANCE)), o1.get(Double.parseDouble(TAG_DISTANCE))); // error
  57.                     // return Double.compare(o1.get(value1), o1.get(value2)); **--> PREM answer, how to use it?**
  58.                 }                  
  59.             });                        
  60.         }          
  61.     }catch (JSONException e){
  62.         e.printStackTrace();
  63.     }      
  64.     return listMotor;      
  65. }
  66.  
  67.  
  68. private double RoundDecimal(double distance, int i) {
  69.     BigDecimal bd = new BigDecimal(distance);
  70.     bd = bd.setScale(i, 6);        
  71.     return bd.doubleValue();
  72. }
  73.  
  74.  
  75. private double hitungJarak(double lat_asal, double lng_asal,
  76.         double lat_tujuan, double lng_tujuan) {
  77.  
  78.     double dist;
  79.     double radius = 6371;
  80.     double dLat = Math.toRadians(lat_tujuan- lat_asal);
  81.     double dLon = Math.toRadians(lng_tujuan - lng_asal);
  82.     double a = Math.sin(dLat/2) * Math.sin(dLat/2)
  83.             + Math.cos(Math.toRadians(lat_asal)) * Math.cos(Math.toRadians(lat_tujuan))
  84.             * Math.sin(dLon/2) * Math.sin(dLon/2);
  85.     double c = 2 * Math.asin(Math.sqrt(a));
  86.     double valueResult = radius * c;
  87.     double km = valueResult/1;
  88.     DecimalFormat newFormat = new DecimalFormat("####");
  89.     int kmInDec = Integer.valueOf(newFormat.format(km));
  90.     dist = radius * c;
  91.     return dist;        
  92. }
  93.  
  94. @Override
  95. protected void onPostExecute(ArrayList<HashMap<String, String>> listMotor) {
  96.     // TODO Auto-generated method stub
  97.  
  98.     super.onPostExecute(listMotor);
  99.     ListAdapter adapter = new SimpleAdapter(getActivity(), listMotor,
  100.             R.layout.list_item, new String[] { TAG_NAMA, TAG_ALAMAT, TAG_JARAK },
  101.             new int[] { R.id.namatb, R.id.alamattb, R.id.jarak });
  102.     setListAdapter(adapter);
  103.  
  104. }    
  105.  }
  106.  
  107.  @Override
  108. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  109.         Bundle savedInstanceState) {
  110.     // TODO Auto-generated method stub
  111.     return inflater.inflate(R.layout.list_fragment, container, false);
  112. }
  113.  
  114.  @Override
  115. public void onListItemClick(ListView l, View v, int position, long id) {
  116.  super.onListItemClick(l, v, position, id);
  117.     String pilih = getListAdapter().getItem(position).toString();
  118.     Toast.makeText(getActivity(),
  119.             pilih,
  120.             Toast.LENGTH_LONG).show();          
  121.  
  122.     Intent i = new Intent(getActivity(), DetailDaftar.class);
  123.     Bundle bundle = new Bundle();
  124.     bundle.putString("Selected", pilih);
  125.  
  126.     i.putExtras(bundle);
  127.     startActivity(i);
  128. }
  129. }
  130.  
  131. //coding di atas ada kesalahan dan perbaiki dgn coding berikut:
  132. @Override
  133. public int compare(HashMap<String, String> o1,
  134.                     HashMap<String, String> o2) {
  135.     return Double.compare(Double.parseDouble(o1.get(TAG_DISTANCE)), Double.parseDouble(o2.get(TAG_DISTANCE)));
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement