add sorting for hamlets

git-svn-id: https://osmand.googlecode.com/svn/trunk@266 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-06-30 15:20:30 +00:00
parent 6a558f1ead
commit 508acd5fe3
2 changed files with 43 additions and 4 deletions

View file

@ -5,6 +5,7 @@ import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -31,6 +32,8 @@ import com.osmand.data.index.IndexConstants.IndexBuildingTable;
import com.osmand.data.index.IndexConstants.IndexCityTable;
import com.osmand.data.index.IndexConstants.IndexStreetNodeTable;
import com.osmand.data.index.IndexConstants.IndexStreetTable;
import com.osmand.osm.LatLon;
import com.osmand.osm.MapUtils;
import com.osmand.osm.Node;
import com.osmand.osm.Way;
@ -46,6 +49,38 @@ public class RegionAddressRepository {
private boolean useEnglishNames = false;
public static class MapObjectNameDistanceComparator implements Comparator<MapObject> {
private final boolean useEnName;
private Collator collator = Collator.getInstance();
private final LatLon location;
public MapObjectNameDistanceComparator(boolean useEnName, LatLon location){
this.useEnName = useEnName;
this.location = location;
}
@Override
public int compare(MapObject object1, MapObject object2) {
if(object1 == null || object2 == null){
return object2 == object1 ? 0 : (object1 == null ? -1 : 1);
} else {
int c = collator.compare(object1.getName(useEnName), object2.getName(useEnName));
if(c == 0 && location != null){
LatLon l1 = object1.getLocation();
LatLon l2 = object2.getLocation();
if(l1 == null || l2 == null){
return l2 == l1 ? 0 : (l1 == null ? -1 : 1);
}
return Double.compare(MapUtils.getDistance(location, l1), MapUtils.getDistance(location, l2));
}
return c;
}
}
}
public boolean initialize(final IProgress progress, File file) {
long start = System.currentTimeMillis();
if(db != null){
@ -234,7 +269,7 @@ public class RegionAddressRepository {
}
}
public void fillWithSuggestedCities(String name, List<MapObject> citiesToFill){
public void fillWithSuggestedCities(String name, List<MapObject> citiesToFill, LatLon currentLocation){
preloadCities();
// essentially index is created that cities towns are first in cities map
int ind = 0;
@ -299,12 +334,16 @@ public class RegionAddressRepository {
Cursor query = db.query(IndexCityTable.getTable(), IndexConstants.generateColumnNames(IndexCityTable.values()),
where.toString(), null, null, null, null);
if (query.moveToFirst()) {
List<City> hamlets = new ArrayList<City>();
do {
citiesToFill.add(parseCityFromCursor(query));
hamlets.add(parseCityFromCursor(query));
} while (query.moveToNext());
Collections.sort(hamlets, new MapObjectNameDistanceComparator(useEnglishNames, currentLocation));
citiesToFill.addAll(hamlets);
}
query.close();
log.debug("Loaded citites " + (citiesToFill.size() - initialsize)); //$NON-NLS-1$
}

View file

@ -32,7 +32,7 @@ public class SearchCityByNameActivity extends SearchByNameAbstractActivity<MapOb
public List<MapObject> getObjects(String filter) {
List<MapObject> l = new ArrayList<MapObject>();
if(region != null){
region.fillWithSuggestedCities(filter, l);
region.fillWithSuggestedCities(filter, l, location);
}
return l;
}