#下面代碼Collections中如何根據照片的經緯度來進行排序?#
/**
* 按照地點進行分組
* @param context
* @param list
* @return
*/
public static List<GroupEntity> setAddressOrder(Context context, List<ParentEntity> list) {
if (list == null || list.isEmpty()) {
return null;
}
//排序前
for (int i = 0; i < list.size(); i++) {
String address = LocationUtil.queryLatLongItudeLocation(context,
list.get(i).getLatitude(), list.get(i).getLongitude());
if(address != null){
Log.e("===>>>", address);
}
}
Collections.sort(list, new Comparator<ParentEntity>() {
@Override
public int compare(ParentEntity lhs, ParentEntity rhs) {
if(1 < Math.abs(lhs.getLatitude() - rhs.getLatitude())
&& 1 < Math.abs(lhs.getLongitude() - rhs.getLongitude())){
return 2;
}else{
return 0;
}
}
});
//排序後
for (int i = 0; i < list.size(); i++) {
String address = LocationUtil.queryLatLongItudeLocation(context,
list.get(i).getLatitude(), list.get(i).getLongitude());
if(address != null){
Log.e("===>>>", address);
}
}
List<GroupEntity> entlist = new ArrayList<GroupEntity>();
// GroupEntity group = null;//用來存儲數據
// for (int i = 0; i < list.size(); i++) {
// ParentEntity entity = list.get(i);
//
// if (group == null
// || 1 < Math.abs(group.latitude - entity.getLatitude())
// && 1 < Math.abs(group.longitude - entity.getLongitude())) {
// group = new GroupEntity();//不相同新建放入
// group.latitude = entity.getLatitude();
// group.longitude = entity.getLongitude();
// entlist.add(group);
// }
// group.list.add(entity);
// }
return entlist;
}
自己解決!!!
/**
* 按照地點進行分組
*
* @param context
* @param list
* @return
*/
public static List<GroupEntity> setAddressOrder(Context context,
List<ParentEntity> list) {
if (list == null || list.isEmpty()) {
return null;
}
List<ParentEntity> temp = new ArrayList<ParentEntity>(list);
List<GroupEntity> result = new ArrayList<GroupEntity>();
while (!temp.isEmpty()) {
GroupEntity ge = new GroupEntity(temp.remove(0));
for (ParentEntity e : temp) {
if (0.5 > Math.abs(ge.latitude - e.getLatitude())
&& 0.5 > Math.abs(ge.longitude - e.getLongitude())) {
ge.list.add(e);
}
}
temp.removeAll(ge.list);
result.add(ge);
}
return result;
}