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"?>
<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_descr">Выберите время, через которое карта вернется к текущей позиции</string>
<string name="search_select_point">Выбрать</string>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="poi_filter_nominatim">Online Nominatim</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_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 lastError = ""; //$NON-NLS-1$
public NameFinderPoiFilter(OsmandApplication 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;
}
@ -42,60 +43,53 @@ public class NameFinderPoiFilter extends PoiFilter {
return searchedAmenities;
}
@Override
public String getSearchArea() {
return ""; //$NON-NLS-1$
public String getQuery() {
return query;
}
@Override
public List<Amenity> initializeNewSearch(double lat, double lon, int firstTimeLimit) {
return searchFurther(lat, lon);
}
@Override
public boolean isSearchFurtherAvailable() {
return true;
public void setQuery(String query) {
this.query = query;
}
@Override
public List<Amenity> searchFurther(double latitude, double longitude) {
searchOnline(latitude, longitude, query);
return searchedAmenities;
}
public String searchOnline(double latitude, double longitude, String filter){
protected List<Amenity> searchAmenities(PoiFilter poiFilter, double lat, double lon, int z, int limit, double topLatitude,
double bottomLatitude, double leftLongitude, double rightLongitude) {
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 {
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();
XmlPullParser parser = Xml.newPullParser();
parser.setInput(stream, "UTF-8"); //$NON-NLS-1$
int eventType;
int namedDepth= 0;
Amenity a = null;
while ((eventType = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("searchresults")) { //$NON-NLS-1$
String err = parser.getAttributeValue("", "error"); //$NON-NLS-1$ //$NON-NLS-2$
if (err != null) {
if (err != null && err.length() > 0) {
lastError = err;
stream.close();
return err;
return searchedAmenities;
}
}
if (parser.getName().equals("named")) { //$NON-NLS-1$
if (parser.getName().equals("place")) { //$NON-NLS-1$
namedDepth++;
if (namedDepth == 1) {
try {
Amenity a = new Amenity();
a = new Amenity();
a.setLocation(Double.parseDouble(parser.getAttributeValue("", "lat")), //$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$
String name = parser.getAttributeValue("", "name"); //$NON-NLS-1$//$NON-NLS-2$
a.setId(Long.parseLong(parser.getAttributeValue("", "place_id"))); //$NON-NLS-1$ //$NON-NLS-2$
String name = parser.getAttributeValue("", "display_name"); //$NON-NLS-1$//$NON-NLS-2$
a.setName(name);
a.setEnName(Junidecode.unidecode(name));
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);
} catch (NullPointerException e) {
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$
}
}
} 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) {
if (parser.getName().equals("named")) { //$NON-NLS-1$
if (parser.getName().equals("place")) { //$NON-NLS-1$
namedDepth--;
if(namedDepth == 0){
a = null;
}
}
}
}
stream.close();
} catch (IOException e) {
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) {
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);
return null;
MapUtils.sortListOfMapObject(searchedAmenities, lat, lon);
return searchedAmenities;
}
public String getLastError() {
return lastError;
}
public List<Amenity> getSearchedAmenities() {

View file

@ -121,6 +121,12 @@ public class PoiFilter {
double bottomLatitude = MapUtils.getLatitudeFromTile(zoom, tileNumberY + 0.5);
double leftLongitude = 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,
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();
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) {
if(!isNameFinderFilter()){
amenityAdapter.getFilter().filter(s);
} else {
searchPOILevel.setEnabled(true);
}
}
@Override
@ -265,12 +274,11 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
if (searchedLocation == null) {
searchedLocation = l;
if (!isNameFinderFilter()) {
runNewSearchQuery(SearchAmenityRequest.buildRequest(l, SearchAmenityRequest.NEW_SEARCH_INIT,
searchFilter.getText().toString()));
runNewSearchQuery(SearchAmenityRequest.buildRequest(l, SearchAmenityRequest.NEW_SEARCH_INIT));
}
handled = true;
} 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;
} else if(location == null || location.distanceTo(l) > MIN_DISTANCE_TO_UPDATE){
handled = true;
@ -281,8 +289,8 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
}
}
if(handled) {
updateSearchPoiTextButton();
location = l;
updateSearchPoiTextButton();
amenityAdapter.notifyDataSetInvalidated();
}
@ -311,8 +319,7 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
private void onLongClick(final Amenity amenity) {
String format = OsmAndFormatter.getPoiSimpleFormat(amenity, SearchPOIActivity.this, settings.USE_ENGLISH_NAMES.get());
if (amenity.getOpeningHours() != null) {
Toast.makeText(this, format + " " + getString(R.string.opening_hours) + " : " + amenity.getOpeningHours(), Toast.LENGTH_LONG)
.show();
Toast.makeText(this, format + " " + getString(R.string.opening_hours) + " : " + amenity.getOpeningHours(), Toast.LENGTH_LONG).show();
}
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 SEARCH_FURTHER = 3;
private int type;
private String filter;
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();
req.type = type;
req.location = l;
req.filter = filter;
return req;
}
@ -479,7 +484,11 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
findViewById(R.id.ProgressBar).setVisibility(View.GONE);
findViewById(R.id.SearchAreaText).setVisibility(View.VISIBLE);
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, "");
showOnMap.setEnabled(amenityAdapter.getCount() > 0);
} else {
@ -492,26 +501,12 @@ public class SearchPOIActivity extends ListActivity implements SensorEventListen
@Override
protected List<Amenity> doInBackground(Void... params) {
if (request.location != null) {
if(filter instanceof NameFinderPoiFilter){
final String message = ((NameFinderPoiFilter) filter).
searchOnline(request.location.getLatitude(), request.location.getLongitude(), request.filter);
if(message != null){
uiHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(SearchPOIActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
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());
} 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();