Slightly improve poi search activity

This commit is contained in:
Victor Shcherb 2011-09-24 22:33:32 +02:00
parent 45d34de01c
commit d45ffc252f
5 changed files with 78 additions and 66 deletions

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="auto_follow_route_never">Никогда</string> <string name="poi_filter_nominatim">Nominatim (интернет)</string>
<string name="auto_follow_route_never">Никогда</string>
<string name="choose_auto_follow_route">Настройки автовозрата карты</string> <string name="choose_auto_follow_route">Настройки автовозрата карты</string>
<string name="choose_auto_follow_route_descr">Выберите время, через которое карта вернется к текущей позиции</string> <string name="choose_auto_follow_route_descr">Выберите время, через которое карта вернется к текущей позиции</string>
<string name="search_select_point">Выбрать</string> <string name="search_select_point">Выбрать</string>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources> <resources>
<string name="poi_filter_nominatim">Online Nominatim</string>
<string name="auto_follow_route_never">Never</string> <string name="auto_follow_route_never">Never</string>
<string name="choose_auto_follow_route">Choose auto follow settings </string> <string name="choose_auto_follow_route">Choose auto follow settings </string>
<string name="choose_auto_follow_route_descr">Choose time interval to continue map following at the location</string> <string name="choose_auto_follow_route_descr">Choose time interval to continue map following at the location</string>

View file

@ -29,10 +29,11 @@ public class NameFinderPoiFilter extends PoiFilter {
private String query = ""; //$NON-NLS-1$ private String query = ""; //$NON-NLS-1$
private String lastError = ""; //$NON-NLS-1$
public NameFinderPoiFilter(OsmandApplication application) { public NameFinderPoiFilter(OsmandApplication application) {
super(null, application); super(null, application);
this.name = application.getString(R.string.poi_filter_namefinder); //$NON-NLS-1$ this.name = application.getString(R.string.poi_filter_nominatim); //$NON-NLS-1$
this.filterId = FILTER_ID; this.filterId = FILTER_ID;
} }
@ -42,60 +43,53 @@ public class NameFinderPoiFilter extends PoiFilter {
return searchedAmenities; return searchedAmenities;
} }
@Override public String getQuery() {
public String getSearchArea() { return query;
return ""; //$NON-NLS-1$
} }
@Override public void setQuery(String query) {
public List<Amenity> initializeNewSearch(double lat, double lon, int firstTimeLimit) { this.query = query;
return searchFurther(lat, lon);
} }
@Override
public boolean isSearchFurtherAvailable() { protected List<Amenity> searchAmenities(PoiFilter poiFilter, double lat, double lon, int z, int limit, double topLatitude,
return true; double bottomLatitude, double leftLongitude, double rightLongitude) {
}
@Override
public List<Amenity> searchFurther(double latitude, double longitude) {
searchOnline(latitude, longitude, query);
return searchedAmenities;
}
public String searchOnline(double latitude, double longitude, String filter){
searchedAmenities.clear(); searchedAmenities.clear();
query = filter;
String q = query + " near " +latitude+","+longitude; //$NON-NLS-1$//$NON-NLS-2$ String viewbox = "viewboxlbrt="+((float) leftLongitude)+","+((float) bottomLatitude)+","+((float) rightLongitude)+","+((float) topLatitude);
try { try {
URL url = new URL("http://gazetteer.openstreetmap.org/namefinder/search.xml?find="+URLEncoder.encode(q)); //$NON-NLS-1$ lastError = "";
String urlq = "http://nominatim.openstreetmap.org/search/"+URLEncoder.encode(query)+ "?format=xml&addressdetails=1&limit=200&bounded=1&"+viewbox;
log.info(urlq);
URL url = new URL(urlq); //$NON-NLS-1$
InputStream stream = url.openStream(); InputStream stream = url.openStream();
XmlPullParser parser = Xml.newPullParser(); XmlPullParser parser = Xml.newPullParser();
parser.setInput(stream, "UTF-8"); //$NON-NLS-1$ parser.setInput(stream, "UTF-8"); //$NON-NLS-1$
int eventType; int eventType;
int namedDepth= 0; int namedDepth= 0;
Amenity a = null;
while ((eventType = parser.next()) != XmlPullParser.END_DOCUMENT) { while ((eventType = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("searchresults")) { //$NON-NLS-1$ if (parser.getName().equals("searchresults")) { //$NON-NLS-1$
String err = parser.getAttributeValue("", "error"); //$NON-NLS-1$ //$NON-NLS-2$ String err = parser.getAttributeValue("", "error"); //$NON-NLS-1$ //$NON-NLS-2$
if (err != null) { if (err != null && err.length() > 0) {
lastError = err;
stream.close(); stream.close();
return err; return searchedAmenities;
} }
} }
if (parser.getName().equals("named")) { //$NON-NLS-1$ if (parser.getName().equals("place")) { //$NON-NLS-1$
namedDepth++; namedDepth++;
if (namedDepth == 1) { if (namedDepth == 1) {
try { try {
Amenity a = new Amenity(); a = new Amenity();
a.setLocation(Double.parseDouble(parser.getAttributeValue("", "lat")), //$NON-NLS-1$//$NON-NLS-2$ a.setLocation(Double.parseDouble(parser.getAttributeValue("", "lat")), //$NON-NLS-1$//$NON-NLS-2$
Double.parseDouble(parser.getAttributeValue("", "lon"))); //$NON-NLS-1$//$NON-NLS-2$ Double.parseDouble(parser.getAttributeValue("", "lon"))); //$NON-NLS-1$//$NON-NLS-2$
a.setId(Long.parseLong(parser.getAttributeValue("", "id"))); //$NON-NLS-1$ //$NON-NLS-2$ a.setId(Long.parseLong(parser.getAttributeValue("", "place_id"))); //$NON-NLS-1$ //$NON-NLS-2$
String name = parser.getAttributeValue("", "name"); //$NON-NLS-1$//$NON-NLS-2$ String name = parser.getAttributeValue("", "display_name"); //$NON-NLS-1$//$NON-NLS-2$
a.setName(name); a.setName(name);
a.setEnName(Junidecode.unidecode(name)); a.setEnName(Junidecode.unidecode(name));
a.setType(AmenityType.OTHER); a.setType(AmenityType.OTHER);
a.setSubType(parser.getAttributeValue("", "category")); //$NON-NLS-1$//$NON-NLS-2$ a.setSubType(parser.getAttributeValue("", "type")); //$NON-NLS-1$//$NON-NLS-2$
searchedAmenities.add(a); searchedAmenities.add(a);
} catch (NullPointerException e) { } catch (NullPointerException e) {
log.info("Invalid attributes", e); //$NON-NLS-1$ log.info("Invalid attributes", e); //$NON-NLS-1$
@ -103,23 +97,38 @@ public class NameFinderPoiFilter extends PoiFilter {
log.info("Invalid attributes", e); //$NON-NLS-1$ log.info("Invalid attributes", e); //$NON-NLS-1$
} }
} }
} else if (a != null && parser.getName().equals(a.getSubType())) {
if (parser.next() == XmlPullParser.TEXT) {
String name = parser.getText();
if (name != null) {
a.setName(name);
a.setEnName(Junidecode.unidecode(name));
}
}
} }
} else if (eventType == XmlPullParser.END_TAG) { } else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("named")) { //$NON-NLS-1$ if (parser.getName().equals("place")) { //$NON-NLS-1$
namedDepth--; namedDepth--;
if(namedDepth == 0){
a = null;
}
} }
} }
} }
stream.close(); stream.close();
} catch (IOException e) { } catch (IOException e) {
log.error("Error loading name finder poi", e); //$NON-NLS-1$ log.error("Error loading name finder poi", e); //$NON-NLS-1$
return getApplication().getString(R.string.input_output_error); //$NON-NLS-1$ lastError = getApplication().getString(R.string.input_output_error); //$NON-NLS-1$
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
log.error("Error parsing name finder poi", e); //$NON-NLS-1$ log.error("Error parsing name finder poi", e); //$NON-NLS-1$
return getApplication().getString(R.string.input_output_error); //$NON-NLS-1$ lastError = getApplication().getString(R.string.input_output_error); //$NON-NLS-1$
} }
MapUtils.sortListOfMapObject(searchedAmenities, latitude, longitude); MapUtils.sortListOfMapObject(searchedAmenities, lat, lon);
return null; return searchedAmenities;
}
public String getLastError() {
return lastError;
} }
public List<Amenity> getSearchedAmenities() { public List<Amenity> getSearchedAmenities() {

View file

@ -121,6 +121,12 @@ public class PoiFilter {
double bottomLatitude = MapUtils.getLatitudeFromTile(zoom, tileNumberY + 0.5); double bottomLatitude = MapUtils.getLatitudeFromTile(zoom, tileNumberY + 0.5);
double leftLongitude = MapUtils.getLongitudeFromTile(zoom, tileNumberX - 0.5); double leftLongitude = MapUtils.getLongitudeFromTile(zoom, tileNumberX - 0.5);
double rightLongitude = MapUtils.getLongitudeFromTile(zoom, tileNumberX + 0.5); double rightLongitude = MapUtils.getLongitudeFromTile(zoom, tileNumberX + 0.5);
return searchAmenities(poiFilter, lat, lon, z, limit, topLatitude, bottomLatitude, leftLongitude, rightLongitude);
}
protected List<Amenity> searchAmenities(PoiFilter poiFilter, double lat, double lon, int z, int limit, double topLatitude,
double bottomLatitude, double leftLongitude, double rightLongitude) {
return application.getResourceManager().searchAmenities(poiFilter, return application.getResourceManager().searchAmenities(poiFilter,
topLatitude, leftLongitude, bottomLatitude, rightLongitude, lat, lon, z, limit); topLatitude, leftLongitude, bottomLatitude, rightLongitude, lat, lon, z, limit);
} }

View file

@ -126,7 +126,14 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
Toast.makeText(SearchPOIActivity.this, R.string.poi_namefinder_query_empty, Toast.LENGTH_LONG).show(); Toast.makeText(SearchPOIActivity.this, R.string.poi_namefinder_query_empty, Toast.LENGTH_LONG).show();
return; return;
} }
runNewSearchQuery(SearchAmenityRequest.buildRequest(location, SearchAmenityRequest.SEARCH_FURTHER, query)); if(filter instanceof NameFinderPoiFilter &&
!Algoritms.objectEquals(((NameFinderPoiFilter) filter).getQuery(), query)){
filter.clearPreviousZoom();
((NameFinderPoiFilter) filter).setQuery(query);
runNewSearchQuery(SearchAmenityRequest.buildRequest(location, SearchAmenityRequest.NEW_SEARCH_INIT));
} else {
runNewSearchQuery(SearchAmenityRequest.buildRequest(location, SearchAmenityRequest.SEARCH_FURTHER));
}
} }
}); });
@ -147,6 +154,8 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
if(!isNameFinderFilter()){ if(!isNameFinderFilter()){
amenityAdapter.getFilter().filter(s); amenityAdapter.getFilter().filter(s);
} else {
searchPOILevel.setEnabled(true);
} }
} }
@Override @Override
@ -265,12 +274,11 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
if (searchedLocation == null) { if (searchedLocation == null) {
searchedLocation = l; searchedLocation = l;
if (!isNameFinderFilter()) { if (!isNameFinderFilter()) {
runNewSearchQuery(SearchAmenityRequest.buildRequest(l, SearchAmenityRequest.NEW_SEARCH_INIT, runNewSearchQuery(SearchAmenityRequest.buildRequest(l, SearchAmenityRequest.NEW_SEARCH_INIT));
searchFilter.getText().toString()));
} }
handled = true; handled = true;
} else if (l.distanceTo(searchedLocation) > MIN_DISTANCE_TO_RESEARCH) { } else if (l.distanceTo(searchedLocation) > MIN_DISTANCE_TO_RESEARCH) {
runNewSearchQuery(SearchAmenityRequest.buildRequest(l, SearchAmenityRequest.SEARCH_AGAIN, searchFilter.getText().toString())); runNewSearchQuery(SearchAmenityRequest.buildRequest(l, SearchAmenityRequest.SEARCH_AGAIN));
handled = true; handled = true;
} else if(location == null || location.distanceTo(l) > MIN_DISTANCE_TO_UPDATE){ } else if(location == null || location.distanceTo(l) > MIN_DISTANCE_TO_UPDATE){
handled = true; handled = true;
@ -281,8 +289,8 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
} }
} }
if(handled) { if(handled) {
updateSearchPoiTextButton();
location = l; location = l;
updateSearchPoiTextButton();
amenityAdapter.notifyDataSetInvalidated(); amenityAdapter.notifyDataSetInvalidated();
} }
@ -311,8 +319,7 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
private void onLongClick(final Amenity amenity) { private void onLongClick(final Amenity amenity) {
String format = OsmAndFormatter.getPoiSimpleFormat(amenity, SearchPOIActivity.this, settings.USE_ENGLISH_NAMES.get()); String format = OsmAndFormatter.getPoiSimpleFormat(amenity, SearchPOIActivity.this, settings.USE_ENGLISH_NAMES.get());
if (amenity.getOpeningHours() != null) { if (amenity.getOpeningHours() != null) {
Toast.makeText(this, format + " " + getString(R.string.opening_hours) + " : " + amenity.getOpeningHours(), Toast.LENGTH_LONG) Toast.makeText(this, format + " " + getString(R.string.opening_hours) + " : " + amenity.getOpeningHours(), Toast.LENGTH_LONG).show();
.show();
} }
AlertDialog.Builder builder = new AlertDialog.Builder(SearchPOIActivity.this); AlertDialog.Builder builder = new AlertDialog.Builder(SearchPOIActivity.this);
@ -441,14 +448,12 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
private static final int NEW_SEARCH_INIT = 2; private static final int NEW_SEARCH_INIT = 2;
private static final int SEARCH_FURTHER = 3; private static final int SEARCH_FURTHER = 3;
private int type; private int type;
private String filter;
private Location location; private Location location;
public static SearchAmenityRequest buildRequest(Location l, int type, String filter){ public static SearchAmenityRequest buildRequest(Location l, int type){
SearchAmenityRequest req = new SearchAmenityRequest(); SearchAmenityRequest req = new SearchAmenityRequest();
req.type = type; req.type = type;
req.location = l; req.location = l;
req.filter = filter;
return req; return req;
} }
@ -479,7 +484,11 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
findViewById(R.id.ProgressBar).setVisibility(View.GONE); findViewById(R.id.ProgressBar).setVisibility(View.GONE);
findViewById(R.id.SearchAreaText).setVisibility(View.VISIBLE); findViewById(R.id.SearchAreaText).setVisibility(View.VISIBLE);
searchPOILevel.setEnabled(filter.isSearchFurtherAvailable()); searchPOILevel.setEnabled(filter.isSearchFurtherAvailable());
if(filter instanceof NameFinderPoiFilter){ if(isNameFinderFilter()){
if(!Algoritms.isEmpty(((NameFinderPoiFilter) filter).getLastError())){
Toast.makeText(SearchPOIActivity.this, ((NameFinderPoiFilter) filter).getLastError(),
Toast.LENGTH_LONG).show();
}
amenityAdapter.setNewModel(result, ""); amenityAdapter.setNewModel(result, "");
showOnMap.setEnabled(amenityAdapter.getCount() > 0); showOnMap.setEnabled(amenityAdapter.getCount() > 0);
} else { } else {
@ -492,26 +501,12 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
@Override @Override
protected List<Amenity> doInBackground(Void... params) { protected List<Amenity> doInBackground(Void... params) {
if (request.location != null) { if (request.location != null) {
if(filter instanceof NameFinderPoiFilter){ if (request.type == SearchAmenityRequest.NEW_SEARCH_INIT) {
final String message = ((NameFinderPoiFilter) filter). return filter.initializeNewSearch(request.location.getLatitude(), request.location.getLongitude(), -1);
searchOnline(request.location.getLatitude(), request.location.getLongitude(), request.filter); } else if (request.type == SearchAmenityRequest.SEARCH_FURTHER) {
if(message != null){ return filter.searchFurther(request.location.getLatitude(), request.location.getLongitude());
uiHandler.post(new Runnable() { } else if (request.type == SearchAmenityRequest.SEARCH_AGAIN) {
@Override
public void run() {
Toast.makeText(SearchPOIActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
return filter.searchAgain(request.location.getLatitude(), request.location.getLongitude()); return filter.searchAgain(request.location.getLatitude(), request.location.getLongitude());
} else {
if (request.type == SearchAmenityRequest.NEW_SEARCH_INIT) {
return filter.initializeNewSearch(request.location.getLatitude(), request.location.getLongitude(), -1);
} else if (request.type == SearchAmenityRequest.SEARCH_FURTHER) {
return filter.searchFurther(request.location.getLatitude(), request.location.getLongitude());
} else if (request.type == SearchAmenityRequest.SEARCH_AGAIN) {
return filter.searchAgain(request.location.getLatitude(), request.location.getLongitude());
}
} }
} }
return Collections.emptyList(); return Collections.emptyList();