计算两个位置之间距离的算法太多了

时间:2016-12-27 17:07:38

标签: android geolocation

首先,我从服务器数据库中获取一些数据并获取一些地址,这些地址反转到位置纬度和经度,所以我可以计算我的位置latlng和他们之间的距离。

但是由于某些原因,Activity在加载数据时冻结,并且使用此功能列表加载速度非常慢。这就是我如何计算两个位置之间的距离:

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");
                            }

任何建议或提示我该怎么办?谢谢!

1 个答案:

答案 0 :(得分: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();
    }
}

希望它有所帮助!