我已将lat和lng存储在firebase数据库中。现在,我想检索这些,然后计算当前位置与这些点之间的距离,然后在适配器中按距离的升序对它们进行排序
我能够检索lat和lng并计算距离并填充firebase recyclerview适配器。但是在此适配器中,未根据距离对项目进行排序。我想根据检索项目后计算出的距离对它们进行排序。
onBindViewHolder内FirebaseRecyclerAdapter中的代码
lat = mLastLocation.getLatitude();
lng = mLastLocation.getLongitude();
lat1 = Double.parseDouble(model.getMylat());
lng1 = Double.parseDouble(model.getMylng());
loadLocationForThisUser(lat, lng, lat1, lng1);
holder.txt_distance.setText(distance);
和用于计算距离的代码
private void loadLocationForThisUser(Double lat, Double lng, Double lat1, Double lng1) {
//Create location from user coordinates
currentUser1 = new Location("");
currentUser1.setLatitude(lat);
currentUser1.setLongitude(lng);
//Create location from friend coordinates
friend1 = new Location("");
friend1.setLatitude(lat1);
friend1.setLongitude(lng1);
distanceNumeric = (int) (currentUser1.distanceTo(friend1)/1000);
Log.i("distanceNumeric", String.valueOf(distanceNumeric));
distance = (new
DecimalFormat("#.#").format((currentUser1.distanceTo(friend1)) / 1000)+ "
kms");
}
当前,输出是根据计算的距离填充的项目,没有任何排序顺序。我希望适配器中将按计算出的距离递增顺序填充项目。