diff --git a/DataExtractionOSM/src/com/osmand/data/Region.java b/DataExtractionOSM/src/com/osmand/data/Region.java index 6c8ceef58d..7e881ac1c6 100644 --- a/DataExtractionOSM/src/com/osmand/data/Region.java +++ b/DataExtractionOSM/src/com/osmand/data/Region.java @@ -110,10 +110,7 @@ public class Region extends MapObject { return closest; } - public List getClosestAmenities(double latitude, double longitude){ - return amenities.getClosestObjects(latitude, longitude); - } - + public DataTileManager getAmenityManager(){ return amenities; } diff --git a/DataExtractionOSM/src/com/osmand/osm/MapUtils.java b/DataExtractionOSM/src/com/osmand/osm/MapUtils.java index cd6f42188c..f0f8eabf4c 100644 --- a/DataExtractionOSM/src/com/osmand/osm/MapUtils.java +++ b/DataExtractionOSM/src/com/osmand/osm/MapUtils.java @@ -2,8 +2,12 @@ package com.osmand.osm; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.List; +import com.osmand.data.MapObject; + /** * This utility class includes : * 1. distance algorithms @@ -155,5 +159,15 @@ public class MapUtils { } } + public static void sortListOfMapObject(List> list, final double lat, final double lon){ + Collections.sort(list, new Comparator>() { + @Override + public int compare(MapObject o1, MapObject o2) { + return Double.compare(MapUtils.getDistance(o1.getLocation(), lat, lon), MapUtils.getDistance(o2.getLocation(), + lat, lon)); + } + }); + } + } diff --git a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java index cb4634224d..437112bfec 100644 --- a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java +++ b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java @@ -16,7 +16,6 @@ import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.EventObject; import java.util.List; import java.util.Map; @@ -82,8 +81,8 @@ import com.osmand.osm.MapUtils; import com.osmand.osm.Node; import com.osmand.osm.Way; import com.osmand.osm.io.IOsmStorageFilter; -import com.osmand.osm.io.OsmStorageWriter; import com.osmand.osm.io.OsmBoundsFilter; +import com.osmand.osm.io.OsmStorageWriter; import com.osmand.swing.MapPanel.MapSelectionArea; public class OsmExtractionUI implements IMapLocationListener { @@ -691,14 +690,9 @@ public class OsmExtractionUI implements IMapLocationListener { public void locationChanged(final double newLatitude, final double newLongitude, Object source){ if (amenitiesTree != null) { Region reg = (Region) amenitiesTree.getModelObject(); - List closestAmenities = reg.getClosestAmenities(newLatitude, newLongitude); - Collections.sort(closestAmenities, new Comparator() { - @Override - public int compare(Amenity o1, Amenity o2) { - return Double.compare(MapUtils.getDistance(o2.getLocation(), newLatitude, newLongitude), MapUtils.getDistance(o2.getLocation(), - newLatitude, newLongitude)); - } - }); + List closestAmenities = reg.getAmenityManager().getClosestObjects(newLatitude, newLongitude, 0, 5); + MapUtils.sortListOfMapObject(closestAmenities, newLatitude, newLongitude); + Map> filter = new TreeMap>(); for (Amenity n : closestAmenities) { diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index bdb37f45cf..6535820da9 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -14,6 +14,8 @@ + diff --git a/OsmAnd/res/drawable/folder.png b/OsmAnd/res/drawable/folder.png new file mode 100644 index 0000000000..6b22a1390c Binary files /dev/null and b/OsmAnd/res/drawable/folder.png differ diff --git a/OsmAnd/res/drawable/poi.png b/OsmAnd/res/drawable/poi.png new file mode 100644 index 0000000000..75a7000231 Binary files /dev/null and b/OsmAnd/res/drawable/poi.png differ diff --git a/OsmAnd/res/layout/search.xml b/OsmAnd/res/layout/search.xml new file mode 100644 index 0000000000..04134a0189 --- /dev/null +++ b/OsmAnd/res/layout/search.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/searchlist.xml b/OsmAnd/res/layout/searchlist.xml new file mode 100644 index 0000000000..1c70eb39d5 --- /dev/null +++ b/OsmAnd/res/layout/searchlist.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index ac3512bfe3..99ccc72d8d 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -19,4 +19,6 @@ Settings Search #CFFACD +search +#FFFFFF diff --git a/OsmAnd/src/com/osmand/OsmandSettings.java b/OsmAnd/src/com/osmand/OsmandSettings.java index a98e9d0962..3b559dc04e 100644 --- a/OsmAnd/src/com/osmand/OsmandSettings.java +++ b/OsmAnd/src/com/osmand/OsmandSettings.java @@ -4,10 +4,12 @@ import java.util.List; import android.content.Context; import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import com.osmand.map.ITileSource; import com.osmand.map.TileSourceManager; import com.osmand.map.TileSourceManager.TileSourceTemplate; +import com.osmand.osm.LatLon; public class OsmandSettings { @@ -28,6 +30,7 @@ public class OsmandSettings { return prefs.getBoolean(SHOW_POI_OVER_MAP, false); } + // this value string is synchronized with android.xml preference name public static final String MAP_TILE_SOURCES = "map_tile_sources"; public static ITileSource getMapTileSource(Context ctx){ @@ -52,8 +55,36 @@ public class OsmandSettings { } return TileSourceManager.getMapnikSource().getName(); } + + // This value is a key for saving last known location shown on the map + public static final String LAST_KNOWN_MAP_LAT = "last_known_map_lat"; + public static final String LAST_KNOWN_MAP_LON = "last_known_map_lon"; + public static final String LAST_KNOWN_MAP_ZOOM = "last_known_map_zoom"; + public static LatLon getLastKnownMapLocation(Context ctx){ + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + float lat = prefs.getFloat(LAST_KNOWN_MAP_LAT, 0); + float lon = prefs.getFloat(LAST_KNOWN_MAP_LON, 0); + return new LatLon(lat, lon); + } + public static void setLastKnownMapLocation(Context ctx, double latitude, double longitude){ + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + Editor edit = prefs.edit(); + edit.putFloat(LAST_KNOWN_MAP_LAT, (float) latitude); + edit.putFloat(LAST_KNOWN_MAP_LON, (float) longitude); + edit.commit(); + } + public static int getLastKnownMapZoom(Context ctx){ + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + return prefs.getInt(LAST_KNOWN_MAP_ZOOM, 3); + } + public static void setLastKnownMapZoom(Context ctx, int zoom){ + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + Editor edit = prefs.edit(); + edit.putInt(LAST_KNOWN_MAP_ZOOM, zoom); + edit.commit(); + } } diff --git a/OsmAnd/src/com/osmand/ResourceManager.java b/OsmAnd/src/com/osmand/ResourceManager.java index 6e2f35b187..974f8e1a60 100644 --- a/OsmAnd/src/com/osmand/ResourceManager.java +++ b/OsmAnd/src/com/osmand/ResourceManager.java @@ -69,9 +69,7 @@ public class ResourceManager { private MapTileDownloader downloader = MapTileDownloader.getInstance(); public AsyncLoadingThread asyncLoadingTiles = new AsyncLoadingThread(); - - public ResourceManager() { // TODO start/stop this thread when needed? @@ -317,6 +315,5 @@ public class ResourceManager { } } System.gc(); - } - + } } diff --git a/OsmAnd/src/com/osmand/activities/MainMenuActivity.java b/OsmAnd/src/com/osmand/activities/MainMenuActivity.java index 18ac314e8e..1c360dd462 100644 --- a/OsmAnd/src/com/osmand/activities/MainMenuActivity.java +++ b/OsmAnd/src/com/osmand/activities/MainMenuActivity.java @@ -21,6 +21,7 @@ public class MainMenuActivity extends Activity { private Button showMap; private Button exitButton; private Button settingsButton; + private Button searchButton; private NotificationManager mNotificationManager; private int APP_NOTIFICATION_ID; @@ -61,6 +62,20 @@ public class MainMenuActivity extends Activity { startActivity(settings); } }); + + searchButton = (Button) findViewById(R.id.SearchButton); + searchButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + Bundle bundle = new Bundle(); + bundle.putString(SearchActivity.ANENITY_TYPE, null); + final Intent search = new Intent(MainMenuActivity.this, + SearchActivity.class); + search.putExtras(bundle); + startActivity(search); + } + }); + exitButton = (Button) findViewById(R.id.ExitButton); exitButton.setOnClickListener(new OnClickListener() { @Override diff --git a/OsmAnd/src/com/osmand/activities/MapActivity.java b/OsmAnd/src/com/osmand/activities/MapActivity.java index da6a394793..576da98408 100644 --- a/OsmAnd/src/com/osmand/activities/MapActivity.java +++ b/OsmAnd/src/com/osmand/activities/MapActivity.java @@ -5,7 +5,6 @@ import java.text.MessageFormat; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; @@ -28,15 +27,13 @@ import com.osmand.OsmandSettings; import com.osmand.R; import com.osmand.ResourceManager; import com.osmand.data.preparation.MapTileDownloader; +import com.osmand.osm.LatLon; import com.osmand.views.OsmandMapTileView; import com.osmand.views.POIMapLayer; import com.osmand.views.PointLocationLayer; public class MapActivity extends Activity implements LocationListener, IMapLocationListener { - private static final String KEY_LAST_LAT = "KEY_LAST_LAT"; - private static final String KEY_LAST_LON = "KEY_LAST_LON"; - private static final String KEY_LAST_ZOOM = "KEY_LAST_ZOOM"; /** Called when the activity is first created. */ private OsmandMapTileView mapView; @@ -52,10 +49,6 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat private POIMapLayer poiMapLayer; private WakeLock wakeLock; - protected void onRestoreInstanceState(Bundle savedInstanceState) { - - } - @Override public void onCreate(Bundle savedInstanceState) { @@ -76,10 +69,11 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat locationLayer = new PointLocationLayer(); mapView.addLayer(locationLayer); - SharedPreferences prefs = getPreferences(MODE_WORLD_READABLE); - if(prefs != null && prefs.contains(KEY_LAST_LAT)){ - mapView.setLatLon(prefs.getFloat(KEY_LAST_LAT, 0f), prefs.getFloat(KEY_LAST_LON, 0f)); - mapView.setZoom(prefs.getInt(KEY_LAST_ZOOM, 3)); + SharedPreferences prefs = getSharedPreferences(OsmandSettings.SHARED_PREFERENCES_NAME, MODE_WORLD_READABLE); + if(prefs != null && prefs.contains(OsmandSettings.LAST_KNOWN_MAP_LAT)){ + LatLon l = OsmandSettings.getLastKnownMapLocation(this); + mapView.setLatLon(l.getLatitude(), l.getLongitude()); + mapView.setZoom(OsmandSettings.getLastKnownMapZoom(this)); } else { LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); Location location = service.getLastKnownLocation(LocationManager.GPS_PROVIDER); @@ -183,12 +177,8 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat super.onPause(); LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); service.removeUpdates(this); - SharedPreferences prefs = getPreferences(MODE_WORLD_READABLE); - Editor edit = prefs.edit(); - edit.putFloat(KEY_LAST_LAT, (float) mapView.getLatitude()); - edit.putFloat(KEY_LAST_LON, (float) mapView.getLongitude()); - edit.putInt(KEY_LAST_ZOOM, mapView.getZoom()); - edit.commit(); + OsmandSettings.setLastKnownMapLocation(this, (float) mapView.getLatitude(), (float) mapView.getLongitude()); + OsmandSettings.setLastKnownMapZoom(this, mapView.getZoom()); if (wakeLock != null) { wakeLock.release(); wakeLock = null; diff --git a/OsmAnd/src/com/osmand/activities/SearchActivity.java b/OsmAnd/src/com/osmand/activities/SearchActivity.java new file mode 100644 index 0000000000..da8e3c9322 --- /dev/null +++ b/OsmAnd/src/com/osmand/activities/SearchActivity.java @@ -0,0 +1,144 @@ +/** + * + */ +package com.osmand.activities; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import android.app.ListActivity; +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; + +import com.osmand.Algoritms; +import com.osmand.OsmandSettings; +import com.osmand.R; +import com.osmand.ResourceManager; +import com.osmand.data.Amenity; +import com.osmand.data.DataTileManager; +import com.osmand.data.Amenity.AmenityType; +import com.osmand.osm.LatLon; +import com.osmand.osm.MapUtils; + +/** + * @author Maxim Frolov + * + */ +public class SearchActivity extends ListActivity { + + public static final String ANENITY_TYPE = "amenity_type"; + + List amenityList = new ArrayList(); + Map> filter; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setContentView(R.layout.search); + createAmenityTypeList(); + Bundle bundle = this.getIntent().getExtras(); + String anemity = bundle.getString(ANENITY_TYPE); + if (anemity != null) { + AmenityType amenityType = findAmenityType(anemity); + createAmenityFilter(); + List list = filter.get(amenityType); + if(list != null) { + setListAdapter(new AmenityAdapter(filter.get(amenityType))); + } + } else { + setListAdapter(new AmenityAdapter(amenityList)); + } + } + + private void createAmenityTypeList() { + for (AmenityType type : AmenityType.values()) { + amenityList.add(Algoritms.capitalizeFirstLetterAndLowercase(type.toString())); + } + + } + + private void createAmenityFilter() { + DataTileManager poiIndex = ResourceManager.getResourceManager().getPoiIndex(); + filter = new TreeMap>(); + LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(this); + List closestAmenities = poiIndex.getClosestObjects(lastKnownMapLocation.getLatitude(), + lastKnownMapLocation.getLongitude(), 0, 5); + MapUtils.sortListOfMapObject(closestAmenities, lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude()); + for (Amenity n : closestAmenities) { + AmenityType type = n.getType(); + if (!filter.containsKey(type)) { + filter.put(type, new ArrayList()); + } + filter.get(type).add(n); + } + + } + + public void onListItemClick(ListView parent, View v, int position, long id) { + AmenityType amenityType = findAmenityType(amenityList.get(position)); + // folder selected + if (amenityType != null) { + Bundle bundle = new Bundle(); + bundle.putString(ANENITY_TYPE, amenityList.get(position)); + Intent newIntent = new Intent(this.getApplicationContext(), SearchActivity.class); + newIntent.putExtras(bundle); + startActivityForResult(newIntent, 0); + } else { + // poi selected + } + } + + private AmenityType findAmenityType(String string) { + for (AmenityType type : AmenityType.values()) { + if (string.equals(Algoritms.capitalizeFirstLetterAndLowercase(type.toString()))) { + return type; + } + } + return null; + + } + + @SuppressWarnings("unchecked") + class AmenityAdapter extends ArrayAdapter { + AmenityAdapter(Object list) { + super(SearchActivity.this, R.layout.searchlist, (List) list); + } + + public View getView(int position, View convertView, ViewGroup parent) { + LayoutInflater inflater = getLayoutInflater(); + View row = inflater.inflate(R.layout.searchlist, parent, false); + TextView label = (TextView) row.findViewById(R.id.label); + ImageView icon = (ImageView) row.findViewById(R.id.icon); + Object model = getModel(position); + if (model instanceof String) { + label.setText((String) model); + icon.setImageResource(R.drawable.folder); + } else if (model instanceof Amenity) { + Amenity anemity = (Amenity) model; + if (anemity != null) { + LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(SearchActivity.this); + int dist = (int) (MapUtils.getDistance(anemity.getLocation(), lastKnownMapLocation.getLatitude(), lastKnownMapLocation + .getLongitude())); + String str = anemity.getStringWithoutType() + " [" + dist + " m ]"; + label.setText(str); + icon.setImageResource(R.drawable.poi); + } + } + return (row); + } + + private Object getModel(int position) { + return (((AmenityAdapter) getListAdapter()).getItem(position)); + } + } + +}