Fix search address

This commit is contained in:
Victor Shcherb 2011-09-13 08:43:57 +02:00
parent b32ea87c04
commit 1ab21d4033
3 changed files with 98 additions and 143 deletions

View file

@ -830,6 +830,7 @@ public class OsmandSettings {
public static final String LAST_SEARCHED_REGION = "last_searched_region"; //$NON-NLS-1$ public static final String LAST_SEARCHED_REGION = "last_searched_region"; //$NON-NLS-1$
public static final String LAST_SEARCHED_CITY = "last_searched_city"; //$NON-NLS-1$ public static final String LAST_SEARCHED_CITY = "last_searched_city"; //$NON-NLS-1$
public static final String LAST_SEARCHED_CITY_NAME = "last_searched_city_name"; //$NON-NLS-1$
public static final String lAST_SEARCHED_POSTCODE= "last_searched_postcode"; //$NON-NLS-1$ public static final String lAST_SEARCHED_POSTCODE= "last_searched_postcode"; //$NON-NLS-1$
public static final String LAST_SEARCHED_STREET = "last_searched_street"; //$NON-NLS-1$ public static final String LAST_SEARCHED_STREET = "last_searched_street"; //$NON-NLS-1$
public static final String LAST_SEARCHED_BUILDING = "last_searched_building"; //$NON-NLS-1$ public static final String LAST_SEARCHED_BUILDING = "last_searched_building"; //$NON-NLS-1$
@ -863,8 +864,8 @@ public class OsmandSettings {
} }
public boolean setLastSearchedRegion(String region, LatLon l) { public boolean setLastSearchedRegion(String region, LatLon l) {
Editor edit = globalPreferences.edit().putString(LAST_SEARCHED_REGION, region).putLong(LAST_SEARCHED_CITY, -1).putString(LAST_SEARCHED_STREET, Editor edit = globalPreferences.edit().putString(LAST_SEARCHED_REGION, region).putLong(LAST_SEARCHED_CITY, -1)
"").putString(LAST_SEARCHED_BUILDING, ""); //$NON-NLS-1$ //$NON-NLS-2$ .putString(LAST_SEARCHED_CITY_NAME, "").putString(LAST_SEARCHED_STREET,"").putString(LAST_SEARCHED_BUILDING, ""); //$NON-NLS-1$ //$NON-NLS-2$
if (globalPreferences.contains(LAST_SEARCHED_INTERSECTED_STREET)) { if (globalPreferences.contains(LAST_SEARCHED_INTERSECTED_STREET)) {
edit.putString(LAST_SEARCHED_INTERSECTED_STREET, ""); //$NON-NLS-1$ edit.putString(LAST_SEARCHED_INTERSECTED_STREET, ""); //$NON-NLS-1$
} }
@ -890,9 +891,13 @@ public class OsmandSettings {
return globalPreferences.getLong(LAST_SEARCHED_CITY, -1); return globalPreferences.getLong(LAST_SEARCHED_CITY, -1);
} }
public boolean setLastSearchedCity(Long cityId) { public String getLastSearchedCityName() {
Editor edit = globalPreferences.edit().putLong(LAST_SEARCHED_CITY, cityId).putString(LAST_SEARCHED_STREET, "").putString( //$NON-NLS-1$ return globalPreferences.getString(LAST_SEARCHED_CITY_NAME, "");
LAST_SEARCHED_BUILDING, ""); //$NON-NLS-1$ }
public boolean setLastSearchedCity(Long cityId, String name) {
Editor edit = globalPreferences.edit().putLong(LAST_SEARCHED_CITY, cityId).putString(LAST_SEARCHED_CITY_NAME, name).
putString(LAST_SEARCHED_STREET, "").putString(LAST_SEARCHED_BUILDING, ""); //$NON-NLS-1$
edit.remove(lAST_SEARCHED_POSTCODE); edit.remove(lAST_SEARCHED_POSTCODE);
if(globalPreferences.contains(LAST_SEARCHED_INTERSECTED_STREET)){ if(globalPreferences.contains(LAST_SEARCHED_INTERSECTED_STREET)){
edit.putString(LAST_SEARCHED_INTERSECTED_STREET, ""); //$NON-NLS-1$ edit.putString(LAST_SEARCHED_INTERSECTED_STREET, ""); //$NON-NLS-1$

View file

@ -3,19 +3,12 @@ package net.osmand.plus.activities.search;
import java.text.MessageFormat; import java.text.MessageFormat;
import net.osmand.data.Building; import net.osmand.Algoritms;
import net.osmand.data.City;
import net.osmand.data.MapObject;
import net.osmand.data.PostCode;
import net.osmand.data.Street;
import net.osmand.osm.LatLon; import net.osmand.osm.LatLon;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.RegionAddressRepository;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.OsmandApplication;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
@ -34,18 +27,19 @@ public class SearchAddressActivity extends Activity {
private Button buildingButton; private Button buildingButton;
private Button navigateTo; private Button navigateTo;
private RegionAddressRepository region = null; private String region = null;
private City city = null; private String city = null;
private PostCode postcode = null; private String postcode = null;
private Street street = null; private String street = null;
private Building building = null; private String building = null;
private Street street2 = null; private String street2 = null;
private boolean radioBuilding = true; private boolean radioBuilding = true;
private Button searchOnline; private Button searchOnline;
private ProgressDialog progressDlg;
private OsmandSettings osmandSettings; private OsmandSettings osmandSettings;
private LatLon searchPoint = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -172,102 +166,88 @@ public class SearchAddressActivity extends Activity {
}); });
} }
public void showOnMap(boolean navigateTo){ public void showOnMap(boolean navigateTo) {
LatLon l = null; if (searchPoint == null) {
return;
}
String historyName = null; String historyName = null;
int zoom = 12; int zoom = 12;
boolean en = osmandSettings.USE_ENGLISH_NAMES.get();
if (street2 != null && street != null) { if (street2 != null && street != null) {
l = region.findStreetIntersection(street, street2); String cityName = postcode != null ? postcode : city;
if(l != null) { historyName = MessageFormat.format(getString(R.string.search_history_int_streets), street, street2,
String cityName = postcode != null? postcode.getName() : city.getName(en); cityName);
historyName = MessageFormat.format(getString(R.string.search_history_int_streets), zoom = 16;
street.getName(en), street2.getName(en), cityName);
zoom = 16;
}
} else if (building != null) { } else if (building != null) {
l = building.getLocation(); String cityName = postcode != null ? postcode : city;
String cityName = postcode != null? postcode.getName() : city.getName(en); historyName = MessageFormat.format(getString(R.string.search_history_building), building, street,
historyName = MessageFormat.format(getString(R.string.search_history_building), building.getName(en), street.getName(en), cityName); cityName);
zoom = 16; zoom = 16;
} else if (street != null) { } else if (street != null) {
l = street.getLocation(); String cityName = postcode != null ? postcode : city;
String cityName = postcode != null? postcode.getName() : city.getName(en); historyName = MessageFormat.format(getString(R.string.search_history_street), street, cityName);
historyName = MessageFormat.format(getString(R.string.search_history_street), street.getName(en), cityName);
zoom = 14; zoom = 14;
} else if (city != null) { } else if (city != null) {
l = city.getLocation(); historyName = MessageFormat.format(getString(R.string.search_history_city), city);
historyName = MessageFormat.format(getString(R.string.search_history_city), city.getName(en));
zoom = 12; zoom = 12;
} }
if (l != null) { if (navigateTo) {
if(navigateTo){ osmandSettings.setPointToNavigate(searchPoint.getLatitude(), searchPoint.getLongitude(), historyName);
osmandSettings.setPointToNavigate(l.getLatitude(), l.getLongitude(), historyName); } else {
} else { osmandSettings.setMapLocationToShow(searchPoint.getLatitude(), searchPoint.getLongitude(), zoom, historyName);
osmandSettings.setMapLocationToShow(l.getLatitude(), l.getLongitude(), zoom, historyName);
}
MapActivity.launchMapActivityMoveToTop(SearchAddressActivity.this);
} }
MapActivity.launchMapActivityMoveToTop(SearchAddressActivity.this);
} }
@Override
protected void onStop() {
if(progressDlg != null){
progressDlg.dismiss();
progressDlg = null;
}
super.onStop();
}
protected void updateBuildingSection(){ protected void updateBuildingSection(){
if(radioBuilding){ if(radioBuilding){
((TextView)findViewById(R.id.BuildingText)).setText(R.string.search_address_building); ((TextView)findViewById(R.id.BuildingText)).setText(R.string.search_address_building);
if(building == null){ if(Algoritms.isEmpty(building)){
((TextView)findViewById(R.id.BuildingButton)).setText(R.string.choose_building); ((TextView)findViewById(R.id.BuildingButton)).setText(R.string.choose_building);
} else { } else {
((TextView)findViewById(R.id.BuildingButton)).setText(building.getName(region.useEnglishNames())); ((TextView)findViewById(R.id.BuildingButton)).setText(building);
} }
} else { } else {
((TextView)findViewById(R.id.BuildingText)).setText(R.string.search_address_street); ((TextView)findViewById(R.id.BuildingText)).setText(R.string.search_address_street);
if(street2 == null){ if(Algoritms.isEmpty(street2)){
((TextView)findViewById(R.id.BuildingButton)).setText(R.string.choose_intersected_street); ((TextView)findViewById(R.id.BuildingButton)).setText(R.string.choose_intersected_street);
} else { } else {
((TextView)findViewById(R.id.BuildingButton)).setText(street2.getName(region.useEnglishNames())); ((TextView)findViewById(R.id.BuildingButton)).setText(street2);
} }
} }
findViewById(R.id.ResetBuilding).setEnabled(building != null || street2 != null); findViewById(R.id.ResetBuilding).setEnabled(!Algoritms.isEmpty(street2) || !Algoritms.isEmpty(building));
} }
protected void updateUI(){ protected void updateUI(){
findViewById(R.id.ResetCountry).setEnabled(region != null); findViewById(R.id.ResetCountry).setEnabled(!Algoritms.isEmpty(region));
if(region == null){ if(Algoritms.isEmpty(region)){
countryButton.setText(R.string.ChooseCountry); countryButton.setText(R.string.ChooseCountry);
} else { } else {
countryButton.setText(region.getName()); countryButton.setText(region);
} }
findViewById(R.id.ResetCity).setEnabled(postcode != null || city != null); findViewById(R.id.ResetCity).setEnabled(postcode != null || city != null);
if(city == null && postcode == null){ if(Algoritms.isEmpty(city) && Algoritms.isEmpty(postcode)){
cityButton.setText(R.string.choose_city); cityButton.setText(R.string.choose_city);
} else { } else {
if(postcode != null){ if(!Algoritms.isEmpty(postcode)){
cityButton.setText(postcode.getName()); cityButton.setText(postcode);
} else { } else {
cityButton.setText(city.getName(region.useEnglishNames())); cityButton.setText(city);
} }
} }
cityButton.setEnabled(region != null); cityButton.setEnabled(!Algoritms.isEmpty(region));
findViewById(R.id.ResetStreet).setEnabled(street != null); findViewById(R.id.ResetStreet).setEnabled(!Algoritms.isEmpty(street));
if(street == null){ if(street == null){
streetButton.setText(R.string.choose_street); streetButton.setText(R.string.choose_street);
} else { } else {
streetButton.setText(street.getName(region.useEnglishNames())); streetButton.setText(street);
} }
streetButton.setEnabled(city != null || postcode != null); streetButton.setEnabled(!Algoritms.isEmpty(city) || !Algoritms.isEmpty(postcode));
buildingButton.setEnabled(street != null); buildingButton.setEnabled(!Algoritms.isEmpty(street));
((RadioGroup)findViewById(R.id.RadioGroup)).setVisibility(street == null ? View.GONE : View.VISIBLE); ((RadioGroup)findViewById(R.id.RadioGroup)).setVisibility(Algoritms.isEmpty(street) ? View.GONE : View.VISIBLE);
if(radioBuilding){ if(radioBuilding){
((RadioButton)findViewById(R.id.RadioBuilding)).setChecked(true); ((RadioButton)findViewById(R.id.RadioBuilding)).setChecked(true);
@ -276,96 +256,66 @@ public class SearchAddressActivity extends Activity {
} }
updateBuildingSection(); updateBuildingSection();
showOnMap.setEnabled(city != null || street != null);
navigateTo.setEnabled(city != null || street != null);
} }
public void loadData(){ public void loadData() {
if (region != null) { if (!Algoritms.isEmpty(region)) {
if(region.useEnglishNames() != osmandSettings.USE_ENGLISH_NAMES.get()){
region.setUseEnglishNames(osmandSettings.USE_ENGLISH_NAMES.get());
}
String postcodeStr = osmandSettings.getLastSearchedPostcode(); String postcodeStr = osmandSettings.getLastSearchedPostcode();
if(postcodeStr != null){ if (!Algoritms.isEmpty(postcodeStr)) {
postcode = region.getPostcode(postcodeStr); postcode = postcodeStr;
} else { } else {
city = region.getCityById(osmandSettings.getLastSearchedCity()); city = osmandSettings.getLastSearchedCityName();
} }
if (postcode != null || city != null) { if (!Algoritms.isEmpty(postcode) || !Algoritms.isEmpty(city)) {
MapObject o = postcode == null ? city : postcode; street = osmandSettings.getLastSearchedStreet();
street = region.getStreetByName(o, osmandSettings.getLastSearchedStreet()); if (Algoritms.isEmpty(street)) {
if (street != null) {
String str = osmandSettings.getLastSearchedIntersectedStreet(); String str = osmandSettings.getLastSearchedIntersectedStreet();
radioBuilding = str == null; radioBuilding = Algoritms.isEmpty(str);
if(str != null){ if (!radioBuilding) {
street2 = region.getStreetByName(o, str); street2 = str;
} else { } else {
building = region.getBuildingByName(street, osmandSettings.getLastSearchedBuilding()); building = osmandSettings.getLastSearchedBuilding();
} }
} }
} }
} }
} }
protected void startLoadDataInThread(String progressMsg){
progressDlg = ProgressDialog.show(this, getString(R.string.loading), progressMsg, true);
new Thread("Loader search data") { //$NON-NLS-1$
@Override
public void run() {
try {
loadData();
} finally {
if (progressDlg != null) {
progressDlg.dismiss();
progressDlg = null;
runOnUiThread(new Runnable() {
@Override
public void run() {
updateUI();
}
});
}
}
}
}.start();
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
region = null;
String lastSearchedRegion = osmandSettings.getLastSearchedRegion();
region = ((OsmandApplication)getApplication()).getResourceManager().getRegionRepository(lastSearchedRegion);
String progressMsg = null;
// try to determine whether progress dialog & new thread needed
if (region != null) { searchPoint = osmandSettings.getLastSearchedPoint();
Long cityId = osmandSettings.getLastSearchedCity(); showOnMap.setEnabled(searchPoint != null);
String postcode = osmandSettings.getLastSearchedPostcode(); navigateTo.setEnabled(searchPoint != null);
if (!region.areCitiesPreloaded()) {
progressMsg = getString(R.string.loading_cities); region = null;
} else if (postcode != null && !region.arePostcodesPreloaded()) {
progressMsg = getString(R.string.loading_postcodes);
} else if (cityId != -1 && region.getCityById(cityId) != null && region.getCityById(cityId).isEmptyWithStreets()) {
progressMsg = getString(R.string.loading_streets_buildings);
} else if (postcode != null && region.getPostcode(postcode) != null && region.getPostcode(postcode).isEmptyWithStreets()) {
progressMsg = getString(R.string.loading_streets_buildings);
} else if (osmandSettings.USE_ENGLISH_NAMES.get() != region.useEnglishNames()) {
progressMsg = getString(R.string.converting_names);
}
}
postcode = null; postcode = null;
city = null; city = null;
street = null; street = null;
building = null; building = null;
region = osmandSettings.getLastSearchedRegion();
loadData();
updateUI();
if (progressMsg != null) { // TODO other can be moved to specific searches
startLoadDataInThread(progressMsg); // if (region != null) {
} else { // Long cityId = osmandSettings.getLastSearchedCity();
loadData(); // String postcode = osmandSettings.getLastSearchedPostcode();
updateUI(); // if (!region.areCitiesPreloaded()) {
} // progressMsg = getString(R.string.loading_cities);
// } else if (postcode != null && !region.arePostcodesPreloaded()) {
// progressMsg = getString(R.string.loading_postcodes);
// } else if (cityId != -1 && region.getCityById(cityId) != null && region.getCityById(cityId).isEmptyWithStreets()) {
// progressMsg = getString(R.string.loading_streets_buildings);
// } else if (postcode != null && region.getPostcode(postcode) != null && region.getPostcode(postcode).isEmptyWithStreets()) {
// progressMsg = getString(R.string.loading_streets_buildings);
// } else if (osmandSettings.USE_ENGLISH_NAMES.get() != region.useEnglishNames()) {
// progressMsg = getString(R.string.converting_names);
// }
// }
} }

View file

@ -53,12 +53,12 @@ public class SearchCityByNameActivity extends SearchByNameAbstractActivity<MapOb
@Override @Override
public void itemSelected(MapObject obj) { public void itemSelected(MapObject obj) {
if (obj instanceof City) { if (obj instanceof City) {
settings.setLastSearchedCity(obj.getId()); settings.setLastSearchedCity(obj.getId(), obj.getName(region.useEnglishNames()));
if (region.getCityById(obj.getId()) == null) { if (region.getCityById(obj.getId()) == null) {
region.addCityToPreloadedList((City) obj); region.addCityToPreloadedList((City) obj);
} }
} else if(obj instanceof PostCode){ } else if(obj instanceof PostCode){
settings.setLastSearchedPostcode(obj.getName()); settings.setLastSearchedPostcode(obj.getName(region.useEnglishNames()));
} }
finish(); finish();