implement search address

git-svn-id: https://osmand.googlecode.com/svn/trunk@106 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-05-31 22:02:21 +00:00
parent 3011d7eeba
commit 8c2ae5e8c2
12 changed files with 220 additions and 43 deletions

View file

@ -26,13 +26,13 @@ public class ToDoConstants {
// 20. Implement save track/route to gpx (?)
// TODO search story :
// 1) Implement loading villages when user types more than 2 symbols
// 2) Find intersection of streets
// 3) Shows progress dialog (?)
// 4) Implement finding buildings
// 5) Show on map
// 6) Show street on map
// 7) Show distance to the village (to distinguish)
// 8) Show message (that user should input more than 2 symbols to get all)
// 9) Loading cities in another thread
// 10. fix bug with landscape layout (search address)
// FIXME Bugs Android :
// 1. When firstly run osmand navigation (from notification bar) show map & go to menu shows desktop.

View file

@ -4,7 +4,7 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout android:id="@+id/LinearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="3dp" android:layout_marginRight="3dp">
<EditText android:text="" android:id="@+id/SearchText" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
<EditText android:text="" android:id="@+id/SearchText" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text|textFilter|textPhonetic"></EditText>
<Button android:text="Reset" android:id="@+id/ResetButton" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"></Button>

View file

@ -3,5 +3,5 @@
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/NameLabel" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:textSize="18dp"/>
android:layout_height="fill_parent" android:textSize="22dp"/>
</LinearLayout>

View file

@ -121,6 +121,7 @@ public class OsmandSettings {
public static final String LAST_SEARCHED_REGION = "last_searched_region";
public static final String LAST_SEARCHED_CITY = "last_searched_city";
public static final String LAST_SEARCHED_STREET = "last_searched_street";
public static final String LAST_SEARCHED_BUILDING = "last_searched_building";
public static String getLastSearchedRegion(Context ctx){
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
@ -151,6 +152,16 @@ public class OsmandSettings {
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
return prefs.edit().putString(LAST_SEARCHED_STREET, street).commit();
}
public static String getLastSearchedBuilding(Context ctx){
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
return prefs.getString(LAST_SEARCHED_BUILDING, "");
}
public static boolean setLastSearchedBuilding(Context ctx, String building){
SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
return prefs.edit().putString(LAST_SEARCHED_BUILDING, building).commit();
}
}

View file

@ -18,6 +18,7 @@ import com.osmand.data.City;
import com.osmand.data.Street;
import com.osmand.data.City.CityType;
import com.osmand.data.index.IndexConstants;
import com.osmand.data.index.IndexConstants.IndexBuildingTable;
import com.osmand.data.index.IndexConstants.IndexCityTable;
import com.osmand.data.index.IndexConstants.IndexStreetTable;
@ -85,6 +86,25 @@ public class RegionAddressRepository {
return null;
}
public void fillWithSuggestedBuildings(Street street, String name, List<Building> buildingsToFill){
preloadBuildings(street);
name = name.toLowerCase();
int ind = 0;
if(name.length() == 0){
buildingsToFill.addAll(street.getBuildings());
return;
}
for (Building building : street.getBuildings()) {
String lowerCase = building.getName().toLowerCase();
if (lowerCase.startsWith(name)) {
buildingsToFill.add(ind, building);
ind++;
} else if (lowerCase.contains(name)) {
buildingsToFill.add(building);
}
}
}
public void fillWithSuggestedStreets(City c, String name, List<Street> streetsToFill){
preloadStreets(c);
name = name.toLowerCase();
@ -104,7 +124,7 @@ public class RegionAddressRepository {
}
}
public void fillWithSuggestedCities(String name, List<City> citiesToFill, List<City> source){
public void fillWithSuggestedCities(String name, List<City> citiesToFill){
preloadCities();
if(name.length() < 3){
EnumSet<CityType> set = EnumSet.of(CityType.CITY, CityType.TOWN);
@ -123,9 +143,9 @@ public class RegionAddressRepository {
}
} else {
// essentially index is created that cities towns are first in cities map
name = name.toLowerCase();
int ind = 0;
Collection<City> src = source == null ? cities.values() : source;
name = name.toLowerCase();
Collection<City> src = cities.values();
for (City c : src) {
String lowerCase = c.getName().toLowerCase();
if (lowerCase.startsWith(name)) {
@ -135,15 +155,53 @@ public class RegionAddressRepository {
citiesToFill.add(c);
}
}
int initialsize = citiesToFill.size();
log.debug("Start loading cities for " +getName() + " filter " + name);
// lower function in SQLite requires ICU extension
name = Algoritms.capitalizeFirstLetterAndLowercase(name);
StringBuilder where = new StringBuilder(80);
where.
append(IndexCityTable.CITY_TYPE.toString()).append(" not in (").
append('\'').append(CityType.valueToString(CityType.CITY)).append('\'').append(", ").
append('\'').append(CityType.valueToString(CityType.TOWN)).append('\'').append(") and ").
append(IndexCityTable.NAME.toString()).append(" LIKE '"+name+"%'");
Cursor query = db.query(IndexCityTable.getTable(), IndexConstants.generateColumnNames(IndexCityTable.values()),
where.toString(), null, null, null, null);
if (query.moveToFirst()) {
do {
citiesToFill.add(parseCityFromCursor(query));
} while (query.moveToNext());
}
query.close();
log.debug("Loaded citites " + (citiesToFill.size() - initialsize));
}
}
public void preloadBuildings(Street str){
// TODO
public void preloadBuildings(Street street){
if (street.getBuildings().isEmpty()) {
Cursor query = db.query(IndexBuildingTable.getTable(), IndexConstants.generateColumnNames(IndexBuildingTable.values()), "? = street",
new String[] { street.getId() + "" }, null, null, null);
log.debug("Start loading buildings for " + street.getName());
if (query.moveToFirst()) {
do {
Building building = new Building();
building.setId(query.getLong(IndexBuildingTable.ID.ordinal()));
building.setLocation(query.getDouble(IndexBuildingTable.LATITUDE.ordinal()), query.getDouble(IndexBuildingTable.LONGITUDE
.ordinal()));
building.setName(query.getString(IndexBuildingTable.NAME.ordinal()));
street.registerBuilding(building);
} while (query.moveToNext());
}
query.close();
log.debug("Loaded " + street.getBuildings().size() + " buildings");
}
}
public void preloadStreets(City city){
if (city.isEmptyWithStreets()) {
log.debug("Start loading streets for " + city.getName());
Cursor query = db.query(IndexStreetTable.getTable(), IndexConstants.generateColumnNames(IndexStreetTable.values()), "? = city",
new String[] { city.getId() + "" }, null, null, null);
if (query.moveToFirst()) {
@ -157,13 +215,35 @@ public class RegionAddressRepository {
} while (query.moveToNext());
}
query.close();
log.debug("Loaded " + city.getStreets().size() + " streets");
}
}
public void registerCity(City city){
cities.put(city.getId(), city);
if(!cityTypes.containsKey(city.getType())){
cityTypes.put(city.getType(), new ArrayList<City>());
}
cityTypes.get(city.getType()).add(city);
}
protected City parseCityFromCursor(Cursor query){
CityType type = CityType.valueFromString(query.getString(IndexCityTable.CITY_TYPE.ordinal()));
if (type != null) {
City city = new City(type);
city.setId(query.getLong(IndexCityTable.ID.ordinal()));
city.setLocation(query.getDouble(IndexCityTable.LATITUDE.ordinal()), query.getDouble(IndexCityTable.LONGITUDE
.ordinal()));
city.setName(query.getString(IndexCityTable.NAME.ordinal()));
return city;
}
return null;
}
public void preloadCities(){
if (cities.isEmpty()) {
log.debug("Start loading cities for " +getName());
// TODO allow cities of all types
StringBuilder where = new StringBuilder();
where.append(IndexCityTable.CITY_TYPE.toString()).append('=').
append('\'').append(CityType.valueToString(CityType.CITY)).append('\'').append(" or ").
@ -173,19 +253,14 @@ public class RegionAddressRepository {
where.toString(), null, null, null, null);
if(query.moveToFirst()){
do {
CityType type = CityType.valueFromString(query.getString(IndexCityTable.CITY_TYPE.ordinal()));
if (type != null) {
City city = new City(type);
city.setId(query.getLong(IndexCityTable.ID.ordinal()));
city.setLocation(query.getDouble(IndexCityTable.LATITUDE.ordinal()), query.getDouble(IndexCityTable.LONGITUDE
.ordinal()));
city.setName(query.getString(IndexCityTable.NAME.ordinal()));
City city = parseCityFromCursor(query);
if (city != null) {
cities.put(city.getId(), city);
if(!cityTypes.containsKey(type)){
cityTypes.put(type, new ArrayList<City>());
if(!cityTypes.containsKey(city.getType())){
cityTypes.put(city.getType(), new ArrayList<City>());
}
cityTypes.get(type).add(city);
cityTypes.get(city.getType()).add(city);
}
} while(query.moveToNext());

View file

@ -12,9 +12,11 @@ import com.osmand.OsmandSettings;
import com.osmand.R;
import com.osmand.RegionAddressRepository;
import com.osmand.ResourceManager;
import com.osmand.activities.MapActivity;
import com.osmand.data.Building;
import com.osmand.data.City;
import com.osmand.data.Street;
import com.osmand.osm.LatLon;
public class SearchAddressActivity extends Activity {
@ -68,6 +70,29 @@ public class SearchAddressActivity extends Activity {
startActivity(new Intent(SearchAddressActivity.this, SearchBuildingByNameActivity.class));
}
});
showOnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LatLon l = null;
int zoom = 12;
if (building != null) {
l = building.getLocation();
zoom = 16;
} else if (street != null) {
l = street.getLocation();
zoom = 14;
} else if (city != null) {
l = city.getLocation();
zoom = 12;
}
if (l != null) {
OsmandSettings.setLastKnownMapLocation(SearchAddressActivity.this, l.getLatitude(), l.getLongitude());
OsmandSettings.setLastKnownMapZoom(SearchAddressActivity.this, zoom);
startActivity(new Intent(SearchAddressActivity.this, MapActivity.class));
}
}
});
findViewById(R.id.ResetBuilding).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
@ -151,6 +176,9 @@ public class SearchAddressActivity extends Activity {
city = region.getCityById(OsmandSettings.getLastSearchedCity(this));
if(city != null){
street = region.getStreetByName(city, OsmandSettings.getLastSearchedStreet(this));
if(street != null){
building = region.getBuildingByName(street, OsmandSettings.getLastSearchedBuilding(this));
}
}
}

View file

@ -9,38 +9,43 @@ import android.widget.TextView;
import com.osmand.OsmandSettings;
import com.osmand.RegionAddressRepository;
import com.osmand.ResourceManager;
import com.osmand.data.Building;
import com.osmand.data.City;
import com.osmand.data.Street;
public class SearchBuildingByNameActivity extends SearchByNameAbstractActivity<Street> {
public class SearchBuildingByNameActivity extends SearchByNameAbstractActivity<Building> {
private RegionAddressRepository region;
private City city;
private Street street;
@Override
protected void onCreate(Bundle savedInstanceState) {
region = ResourceManager.getResourceManager().getRegionRepository(OsmandSettings.getLastSearchedRegion(this));
if(region != null){
city = region.getCityById(OsmandSettings.getLastSearchedCity(this));
if(city != null){
street = region.getStreetByName(city, OsmandSettings.getLastSearchedStreet(this));
}
}
super.onCreate(savedInstanceState);
}
@Override
public List<Street> getObjects() {
List<Street> l = new ArrayList<Street>();
if(city != null){
region.fillWithSuggestedStreets(city, "", l);
public List<Building> getObjects(String filter) {
List<Building> l = new ArrayList<Building>();
if(street != null){
region.fillWithSuggestedBuildings(street, filter, l);
}
return l;
}
@Override
public void updateTextView(Street obj, TextView txt) {
public void updateTextView(Building obj, TextView txt) {
txt.setText(obj.getName());
}
@Override
public void itemSelected(Street obj) {
OsmandSettings.setLastSearchedStreet(this, obj.getName());
public void itemSelected(Building obj) {
OsmandSettings.setLastSearchedBuilding(this, obj.getName());
finish();
}

View file

@ -4,11 +4,14 @@ import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
@ -16,16 +19,51 @@ import com.osmand.R;
public abstract class SearchByNameAbstractActivity<T> extends ListActivity {
private EditText searchText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.search_by_name);
NamesAdapter namesAdapter = new NamesAdapter(getObjects());
NamesAdapter namesAdapter = new NamesAdapter(getObjects(""));
setListAdapter(namesAdapter);
searchText = (EditText) findViewById(R.id.SearchText);
searchText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
setText(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
findViewById(R.id.ResetButton).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
searchText.setText("");
}
});
}
public Editable getFilter(){
return searchText.getText();
}
public void setText(String filter){
((NamesAdapter)getListAdapter()).clear();
for(T o : getObjects(filter)){
((NamesAdapter)getListAdapter()).add(o);
}
}
public abstract List<T> getObjects();
public abstract List<T> getObjects(String filter);
public abstract void updateTextView(T obj, TextView txt);
@ -36,8 +74,8 @@ public abstract class SearchByNameAbstractActivity<T> extends ListActivity {
protected void onListItemClick(ListView l, View v, int position, long id) {
T repo = (T) getListAdapter().getItem(position);
itemSelected(repo);
}
class NamesAdapter extends ArrayAdapter<T> {
NamesAdapter(List<T> list) {
@ -45,8 +83,13 @@ public abstract class SearchByNameAbstractActivity<T> extends ListActivity {
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.searchbyname_list, parent, false);
View row;
if (convertView != null) {
row = convertView;
} else {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.searchbyname_list, parent, false);
}
TextView label = (TextView) row.findViewById(R.id.NameLabel);
updateTextView(getItem(position), label);
return row;

View file

@ -10,32 +10,44 @@ import com.osmand.OsmandSettings;
import com.osmand.RegionAddressRepository;
import com.osmand.ResourceManager;
import com.osmand.data.City;
import com.osmand.osm.LatLon;
import com.osmand.osm.MapUtils;
public class SearchCityByNameActivity extends SearchByNameAbstractActivity<City> {
private RegionAddressRepository region;
private LatLon location;
@Override
protected void onCreate(Bundle savedInstanceState) {
region = ResourceManager.getResourceManager().getRegionRepository(OsmandSettings.getLastSearchedRegion(this));
location = OsmandSettings.getLastKnownMapLocation(this);
super.onCreate(savedInstanceState);
}
@Override
public List<City> getObjects() {
public List<City> getObjects(String filter) {
List<City> l = new ArrayList<City>();
if(region != null){
region.fillWithSuggestedCities("", l, null);
region.fillWithSuggestedCities(filter, l);
}
return l;
}
@Override
public void updateTextView(City obj, TextView txt) {
txt.setText(obj.getName());
if(getFilter().length() > 2){
txt.setText(obj.getName() + " - " + MapUtils.getFormattedDistance((int) MapUtils.getDistance(obj.getLocation(), location)));
} else {
txt.setText(obj.getName());
}
}
@Override
public void itemSelected(City obj) {
OsmandSettings.setLastSearchedCity(this, obj.getId());
if(region.getCityById(obj.getId()) == null){
region.registerCity(obj);
}
finish();
}

View file

@ -129,8 +129,11 @@ public class SearchPOIActivity extends ListActivity {
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.searchpoi_list, parent, false);
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.searchpoi_list, parent, false);
}
TextView label = (TextView) row.findViewById(R.id.poi_label);
TextView distanceLabel = (TextView) row.findViewById(R.id.poidistance_label);
ImageView icon = (ImageView) row.findViewById(R.id.poi_icon);

View file

@ -12,7 +12,7 @@ import com.osmand.ResourceManager;
public class SearchRegionByNameActivity extends SearchByNameAbstractActivity<RegionAddressRepository> {
@Override
public List<RegionAddressRepository> getObjects() {
public List<RegionAddressRepository> getObjects(String filter) {
return new ArrayList<RegionAddressRepository>(ResourceManager.getResourceManager().getAddressRepositories());
}

View file

@ -25,10 +25,10 @@ public class SearchStreetByNameActivity extends SearchByNameAbstractActivity<Str
}
@Override
public List<Street> getObjects() {
public List<Street> getObjects(String filter) {
List<Street> l = new ArrayList<Street>();
if(city != null){
region.fillWithSuggestedStreets(city, "", l);
region.fillWithSuggestedStreets(city, filter, l);
}
return l;
}