2016-12-27 92 views
0

首先,我是從服務器的數據庫中獲取一些數據,並得到了一些我敢倒車到位置的經度和緯度的地址,所以我的位置之間的距離後,我可以計算latlng和他們的。算法兩個位置之間的距離計算正在太多

但出於某種原因活動凍結在加載數據和列表加載此功能非常緩慢。這是我如何計算兩個位置之間的距離:

AndroidNetworking.get(AppConfig.GET_FIELDS_ORDER_BY_CITY.replace("city", tvCurrentLocation.getText().toString())) 
      .setPriority(Priority.IMMEDIATE) 
      .build().getAsJSONObject(new JSONObjectRequestListener() { 
     @Override 
     public void onResponse(JSONObject response) { 
      mFieldList.clear(); 
      try { 
       if (!response.getBoolean("error")) { 
        JSONArray fieldsArray = response.getJSONArray("fields"); 
        for (int i = 0; i < fieldsArray.length(); i++) { 
         JSONObject fieldObj = fieldsArray.getJSONObject(i); 
         Field field = new Field(); 
         ... 
         field.setCertificate(fieldObj.getString("certificate")); 

         Log.d("Location", fieldObj.getString("address")); 

         // If i delete from here 
         if (!fieldObj.getString("address").isEmpty()) { 
          LatLng latLng = AppUtils.getLocationFromAddress(SearchActivityV2.this, 
            fieldObj.getString("address")); 

          float dist = AppUtils.distFrom(44.8029925f, 20.495337f, (float) latLng.latitude, (float) latLng.longitude); 
          DecimalFormat df = new DecimalFormat("####0.0"); 
          if (dist > 1000) { 
           double distance = AppUtils.calculationByDistance(new LatLng(44.8029925, 20.495337), latLng); 
           field.setDistance(df.format(distance) + " km"); 
          } else { 
           String newValue = Double.toString(Math.floor(dist)); 
           field.setDistance(newValue + " m"); 
          } 
         } else { 
          field.setDistance("1000"); 
         } 
         mFieldList.add(field); 
        } 
        // to here, list would load immediately, so 
        // something is not right with this 
        mFieldsListAdapter.notifyDataSetChanged(); 
       } 
      } catch (JSONException e) { 
       Log.e(getClass().getSimpleName(), e.getMessage()); 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onError(ANError anError) { 
      Log.e(getClass().getSimpleName(), anError.getMessage()); 
     } 
    });ield.setDistance(df.format(distance) + " km"); 
          } else { 
           String newValue = Double.toString(Math.floor(dist)); 
           field.setDistance(newValue + " m"); 
          } 

任何建議或提示我該怎麼辦?謝謝!

+0

嘗試使用'Location.distanceBetween()'方法等中的[hanoo] [本](http://stackoverflow.com/a/15351351/6950238)應答(http://stackoverflow.com/users/1291650/hanoo) –

回答

1

「onResponse」方法適用於主線程,所以數據解析和重計算可能會凍結應用程序的UI線程。 嘗試移動所有的解析代碼和計算的AsyncTask的「doInBackground」和更新您的適配器「onPostExecute」,因爲你必須在UI線程來執行此操作。

代碼示例:

private class ResponseParser extends AsyncTask<JSONObject, Void, Void> { 

    @Override 
    protected Void doInBackground(JSONObject... params) throws JSONException { 
     mFieldList.clear(); 
     JSONArray fieldsArr = params[0].getJSONArray("fields"); 
     if (!response.getBoolean("error")) { 
      JSONArray fieldsArray = response.getJSONArray("fields"); 
      for (int i = 0; i < fieldsArray.length(); i++) { 
       JSONObject fieldObj = fieldsArray.getJSONObject(i); 
       Field field = new Field(); 
       ... 
       field.setCertificate(fieldObj.getString("certificate")); 

       Log.d("Location", fieldObj.getString("address")); 

       // If i delete from here 
       if (!fieldObj.getString("address").isEmpty()) { 
        LatLng latLng = AppUtils.getLocationFromAddress(SearchActivityV2.this, 
          fieldObj.getString("address")); 

        float dist = AppUtils.distFrom(44.8029925f, 20.495337f, (float) latLng.latitude, (float) latLng.longitude); 
        DecimalFormat df = new DecimalFormat("####0.0"); 
        if (dist > 1000) { 
         double distance = AppUtils.calculationByDistance(new LatLng(44.8029925, 20.495337), latLng); 
         field.setDistance(df.format(distance) + " km"); 
        } else { 
         String newValue = Double.toString(Math.floor(dist)); 
         field.setDistance(newValue + " m"); 
        } 
       } else { 
        field.setDistance("1000"); 
       } 
       mFieldList.add(field); 
      } 
      // to here, list would load immediately, so 
      // something is not right with this 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     mFieldsListAdapter.notifyDataSetChanged(); 
    } 
} 

希望它能幫助!