add poi filters functionality

git-svn-id: https://osmand.googlecode.com/svn/trunk@153 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-06-12 07:24:25 +00:00
parent 2304e94369
commit d903637801
4 changed files with 84 additions and 55 deletions

View file

@ -8,6 +8,7 @@ import java.util.Map;
import com.osmand.data.Amenity;
import com.osmand.data.AmenityType;
import com.osmand.data.index.IndexConstants.IndexPoiTable;
import com.osmand.osm.MapUtils;
public class PoiFilter {
@ -21,6 +22,12 @@ public class PoiFilter {
private String name;
private final boolean isStandardFilter;
private final static int finalZoom = 8;
private final static int initialZoom = 13;
private final static int maxCount = 200;
private int zoom = initialZoom;
// constructor for standard filters
public PoiFilter(AmenityType type){
isStandardFilter = true;
@ -48,23 +55,38 @@ public class PoiFilter {
public boolean isSearchFurtherAvailable(){
return false;
return zoom > finalZoom;
}
public List<Amenity> searchFurther(){
return null;
public List<Amenity> searchFurther(double latitude, double longitude){
zoom --;
List<Amenity> amenityList = ResourceManager.getResourceManager().searchAmenities(this, latitude, longitude, zoom, -1);
MapUtils.sortListOfMapObject(amenityList, latitude, longitude);
return amenityList;
}
public String getSearchArea(){
return null;
if(zoom <= 15){
int d = (int) (1.5 * (1 << (zoom - 15)));
return " < " + d + " km";
} else {
return " < 500 m";
}
}
public List<Amenity> getLastSearchedResults(){
return null;
}
public List<Amenity> initializeNewSearch(double lat, double lon){
return null;
public List<Amenity> initializeNewSearch(double lat, double lon, int firstTimeLimit){
zoom = initialZoom;
if(areAllTypesAccepted()){
zoom += 2;
}
List<Amenity> amenityList = ResourceManager.getResourceManager().searchAmenities(this, lat, lon, zoom, maxCount);
MapUtils.sortListOfMapObject(amenityList, lat, lon);
while (amenityList.size() > firstTimeLimit) {
amenityList.remove(amenityList.size() - 1);
}
return amenityList;
}
public String getName(){
@ -90,6 +112,18 @@ public class PoiFilter {
return acceptedTypes.get(type) == null;
}
public boolean areAllTypesAccepted(){
if(AmenityType.values().length == acceptedTypes.size()){
for(AmenityType a : acceptedTypes.keySet()){
if(acceptedTypes.get(a) != null){
return false;
}
}
return true;
}
return false;
}
public void setTypeToAccept(AmenityType type, boolean accept){
if(accept){
@ -100,17 +134,8 @@ public class PoiFilter {
}
public String buildSqlWhereFilter(){
if(AmenityType.values().length == acceptedTypes.size()){
boolean wildcard = true;
for(AmenityType a : acceptedTypes.keySet()){
if(acceptedTypes.get(a) != null){
wildcard = false;
break;
}
}
if(wildcard){
return null;
}
if(areAllTypesAccepted()){
return null;
}
if(acceptedTypes.size() == 0){
return "1 > 1";

View file

@ -1,7 +1,9 @@
package com.osmand;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.osmand.data.AmenityType;
@ -28,11 +30,35 @@ public class PoiFiltersHelper {
return null;
}
public static List<PoiFilter> getUserDefinedDefaultFilters(Context ctx){
List<PoiFilter> filters = new ArrayList<PoiFilter>();
Map<AmenityType, List<String>> types = new LinkedHashMap<AmenityType, List<String>>();
List<String> list = new ArrayList<String>();
list.add("fuel");
list.add("car_wash");
list.add("car_repair");
types.put(AmenityType.TRANSPORTATION, list);
list = new ArrayList<String>();
list.add("car");
types.put(AmenityType.SHOP, list);
filters.add(new PoiFilter("Car aid", null, types));
types.clear();
return filters;
}
private static List<PoiFilter> cacheUserDefinedFilters;
public static List<PoiFilter> getUserDefinedPoiFilters(Context ctx){
if(cacheUserDefinedFilters == null){
cacheUserDefinedFilters = new ArrayList<PoiFilter>();
// TODO
cacheUserDefinedFilters.addAll(getUserDefinedDefaultFilters(ctx));
}
return cacheUserDefinedFilters;
}
@ -41,6 +67,8 @@ public class PoiFiltersHelper {
public static List<PoiFilter> getOsmDefinedPoiFilters(Context ctx){
if(cacheOsmDefinedFilters == null){
cacheOsmDefinedFilters = new ArrayList<PoiFilter>();
// for test purposes
cacheOsmDefinedFilters.addAll(getUserDefinedPoiFilters(ctx));
cacheOsmDefinedFilters.add(new PoiFilter(null));
for(AmenityType t : AmenityType.values()){
cacheOsmDefinedFilters.add(new PoiFilter(t));

View file

@ -371,9 +371,9 @@ public class ResourceManager {
}
sleep(750);
} catch (InterruptedException e) {
log.error(e);
log.error(e, e);
} catch (RuntimeException e){
log.error(e);
log.error(e, e);
}
}
}

View file

@ -25,7 +25,6 @@ import com.osmand.OsmandSettings;
import com.osmand.PoiFilter;
import com.osmand.PoiFiltersHelper;
import com.osmand.R;
import com.osmand.ResourceManager;
import com.osmand.activities.MapActivity;
import com.osmand.data.Amenity;
import com.osmand.osm.LatLon;
@ -42,10 +41,6 @@ public class SearchPOIActivity extends ListActivity {
private List<Amenity> amenityList;
private Button searchPOILevel;
private final static int maxCount = 100;
private final static int finalZoom = 8;
private final static int limitOfClosest = 30;
private int zoom = 13;
private PoiFilter filter;
@ -61,17 +56,9 @@ public class SearchPOIActivity extends ListActivity {
searchPOILevel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ResourceManager resourceManager = ResourceManager.getResourceManager();
if (zoom > finalZoom) {
--zoom;
}
amenityList = resourceManager.searchAmenities(filter, lastKnownMapLocation.getLatitude(), lastKnownMapLocation
.getLongitude(), zoom, -1);
if (amenityList != null) {
MapUtils.sortListOfMapObject(amenityList, lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude());
amenityAdapter.setNewModel(amenityList);
}
searchPOILevel.setEnabled(zoom > finalZoom);
amenityList = filter.searchFurther(lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude());
amenityAdapter.setNewModel(amenityList);
searchPOILevel.setEnabled(filter.isSearchFurtherAvailable());
}
});
@ -79,23 +66,11 @@ public class SearchPOIActivity extends ListActivity {
Bundle bundle = this.getIntent().getExtras();
String filterId = bundle.getString(AMENITY_FILTER);
if (filterId != null) {
ResourceManager resourceManager = ResourceManager.getResourceManager();
lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(this);
filter = PoiFiltersHelper.getFilterById(this, filterId);
amenityList = resourceManager.searchAmenities(filter, lastKnownMapLocation.getLatitude(), lastKnownMapLocation
.getLongitude(), zoom, maxCount);
if (amenityList != null) {
MapUtils.sortListOfMapObject(amenityList, lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude());
// TODO filter closest pois
if(filter.isStandardFilter()){
while (amenityList.size() > limitOfClosest) {
amenityList.remove(amenityList.size() - 1);
}
}
amenityAdapter = new AmenityAdapter(amenityList);
setListAdapter(amenityAdapter);
}
amenityAdapter = new AmenityAdapter(filter.initializeNewSearch(lastKnownMapLocation.getLatitude(),
lastKnownMapLocation.getLongitude(), 40));
setListAdapter(amenityAdapter);
}
// ListActivity has a ListView, which you can get with:
ListView lv = getListView();
@ -161,7 +136,8 @@ public class SearchPOIActivity extends ListActivity {
} else {
icon.setImageResource(R.drawable.closed_poi);
}
distanceLabel.setText(" " + dist + " m ");
distanceLabel.setText(" " + MapUtils.getFormattedDistance(dist));
return (row);
}
}