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

View file

@ -1,7 +1,9 @@
package com.osmand; package com.osmand;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.osmand.data.AmenityType; import com.osmand.data.AmenityType;
@ -28,11 +30,35 @@ public class PoiFiltersHelper {
return null; 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; private static List<PoiFilter> cacheUserDefinedFilters;
public static List<PoiFilter> getUserDefinedPoiFilters(Context ctx){ public static List<PoiFilter> getUserDefinedPoiFilters(Context ctx){
if(cacheUserDefinedFilters == null){ if(cacheUserDefinedFilters == null){
cacheUserDefinedFilters = new ArrayList<PoiFilter>(); cacheUserDefinedFilters = new ArrayList<PoiFilter>();
// TODO // TODO
cacheUserDefinedFilters.addAll(getUserDefinedDefaultFilters(ctx));
} }
return cacheUserDefinedFilters; return cacheUserDefinedFilters;
} }
@ -41,6 +67,8 @@ public class PoiFiltersHelper {
public static List<PoiFilter> getOsmDefinedPoiFilters(Context ctx){ public static List<PoiFilter> getOsmDefinedPoiFilters(Context ctx){
if(cacheOsmDefinedFilters == null){ if(cacheOsmDefinedFilters == null){
cacheOsmDefinedFilters = new ArrayList<PoiFilter>(); cacheOsmDefinedFilters = new ArrayList<PoiFilter>();
// for test purposes
cacheOsmDefinedFilters.addAll(getUserDefinedPoiFilters(ctx));
cacheOsmDefinedFilters.add(new PoiFilter(null)); cacheOsmDefinedFilters.add(new PoiFilter(null));
for(AmenityType t : AmenityType.values()){ for(AmenityType t : AmenityType.values()){
cacheOsmDefinedFilters.add(new PoiFilter(t)); cacheOsmDefinedFilters.add(new PoiFilter(t));

View file

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