2015-04-12 79 views
0

我從網上獲取我的JSON數據。有許多對象,其中一個是經度和緯度。我可以計算確切的距離,但..按最近的位置對列表視圖進行排序

如何排序列表視圖以顯示最近的位置第一。

MainActivity

public class MainActivity extends ActionBarActivity { 

    private ListView lv_nearestShops; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     GPSTracker gps; 

     this.lv_nearestShops = (ListView) this.findViewById(R.id.lv_nearestShops); 

     gps = new GPSTracker(MainActivity.this); 

     if(gps.canGetLocation()) { 
      double latitude = gps.getLatitude(); 
      double longitude = gps.getLongtitude(); 

      Log.d("Location = ", "Latitude: " + latitude + " - Longitude : " + longitude); 
     } 

     new GetAllShopsTask().execute(new ApiConnector()); 


    } 

    public void setListAdapter(JSONArray jsonArray){ 

     this.lv_nearestShops.setAdapter(new lv_nearestShops_adapter(jsonArray, this)); 
    } 

    private class GetAllShopsTask extends AsyncTask<ApiConnector,Long,JSONArray> 
    { 
     @Override 
     protected JSONArray doInBackground(ApiConnector... params) { 
      return params[0].GetAllShops(); 
     } 

     @Override 
     protected void onPostExecute(JSONArray jsonArray) { 
      setListAdapter(jsonArray); 

     } 
    } 


} 

ApiConnector

public class ApiConnector { 

    public JSONArray GetAllShops() { 
     String url = ""; //php script 

     HttpEntity httpEntity = null; 

     try { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 

      httpEntity = httpResponse.getEntity(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     JSONArray jsonArray = null; 
     if (httpEntity != null) { 
      try { 
       String entityResponse = EntityUtils.toString(httpEntity); 

       Log.e("Entity Response : ", entityResponse); 

       jsonArray = new JSONArray(entityResponse); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return jsonArray; 
    } 
} 

的ListView適配器

public class lv_nearestShops_adapter extends BaseAdapter { 

    private JSONArray dataArray; 
    private Activity activity; 

    double latA; 
    double lngA; 
    double latB; 
    double lngB; 

    private static LayoutInflater inflater = null; 

    public lv_nearestShops_adapter(JSONArray jsonArray, Activity a) 
    { 
     this.dataArray = jsonArray; 
     this.activity = a; 

     inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     return this.dataArray.length(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.lv_nearestshop_layout, null); 
      viewHolder = new ViewHolder(); 

      viewHolder.name = (TextView) convertView.findViewById(R.id.itemName); 
      viewHolder.street = (TextView) convertView.findViewById(R.id.itemStreet); 
      viewHolder.distance = (TextView) convertView.findViewById(R.id.itemDistance); 

      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     try { 
      JSONObject jsonObject = this.dataArray.getJSONObject(position); 

      // naam 
      viewHolder.name.setText(jsonObject.getString("name")); 
      // street 
      viewHolder.street.setText(jsonObject.getString("street")); 

      // distance 

      latB = jsonObject.getDouble("latitude"); 
      lngB = jsonObject.getDouble("longitude"); 

      float distance; 

      GPSTracker gps; 

      Context context = parent.getContext(); 
      gps = new GPSTracker(context); 

      if(gps.canGetLocation()) { 
       latA = gps.getLatitude(); 
       lngA = gps.getLongtitude(); 

       Log.e("Location = ", "Latitude: " + latA + " - Longitude : " + lngA); 
      } 

      Location locationA = new Location("point A"); 
      locationA.setLatitude(latA); 
      locationA.setLongitude(lngA); 
      Location locationB = new Location("point B"); 
      locationB.setLatitude(latB); 
      locationB.setLongitude(lngB); 
      distance = locationA.distanceTo(locationB); 

      int iDistance= (int) distance; 

      if(iDistance > 1000) { 
       int result = iDistance/1000; 
       viewHolder.distance.setText(String.valueOf(result) + " km"); 
      } else { 
       viewHolder.distance.setText(String.valueOf(iDistance) + " m"); 
      } 

      jsonObject.put("distance", iDistance); 


     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     return convertView; 
    } 

    private class ViewHolder{ 
     private TextView name; 
     private TextView street; 
     private TextView distance; 
    } 

} 

回答

1

一種方式將是對你JSON陣列包含用於JSON對象的內容的包裝類標準的ArrayList轉換。 e.g

class Location 
{ 
    JSONObject jsonObject; 

    public Location(JSONObject jsonObj) 
    { 
     this.jsonObject = jsonObj 
    } 

    long getLongitude() 
    { 
     // get longitude value from json object 
    } 

    long getLatitude() 
    { 
     // get latitude value from json object 
    } 


    // .. any other info 
} 


List<Location> locationList = new ArrayList<Location>(); 

此列表按距離排序,以當前的位置,使用經度,存儲在Location類緯度創建一個比較級。然後Collection.sort(list,yourComparator)將爲您排序列表。

改變你的列表視圖來代替使用locationList對象。

0

您可以calcu用戶的位置,就像進入LAT-長JSON數據之間的距離後期:

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { 
var R = 6371; // Radius of the earth in km 
    var dLat = deg2rad(lat2-lat1); // deg2rad below 
    var dLon = deg2rad(lon2-lon1); 
    var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) + 
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2) 
    ; 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    return d; 
} 

然後,你可以存儲的距離在陣列和陣列排序。在列表視圖中顯示排序的數據。

Courtesy

相關問題