add search POI

git-svn-id: https://osmand.googlecode.com/svn/trunk@73 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
frolovmo 2010-05-21 06:43:01 +00:00
parent a91d036274
commit 2d49463456
14 changed files with 241 additions and 36 deletions

View file

@ -110,9 +110,6 @@ public class Region extends MapObject<Entity> {
return closest;
}
public List<Amenity> getClosestAmenities(double latitude, double longitude){
return amenities.getClosestObjects(latitude, longitude);
}
public DataTileManager<Amenity> getAmenityManager(){
return amenities;

View file

@ -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<? extends MapObject<?>> list, final double lat, final double lon){
Collections.sort(list, new Comparator<MapObject<?>>() {
@Override
public int compare(MapObject<?> o1, MapObject<?> o2) {
return Double.compare(MapUtils.getDistance(o1.getLocation(), lat, lon), MapUtils.getDistance(o2.getLocation(),
lat, lon));
}
});
}
}

View file

@ -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<Amenity> closestAmenities = reg.getClosestAmenities(newLatitude, newLongitude);
Collections.sort(closestAmenities, new Comparator<Amenity>() {
@Override
public int compare(Amenity o1, Amenity o2) {
return Double.compare(MapUtils.getDistance(o2.getLocation(), newLatitude, newLongitude), MapUtils.getDistance(o2.getLocation(),
newLatitude, newLongitude));
}
});
List<Amenity> closestAmenities = reg.getAmenityManager().getClosestObjects(newLatitude, newLongitude, 0, 5);
MapUtils.sortListOfMapObject(closestAmenities, newLatitude, newLongitude);
Map<AmenityType, List<Amenity>> filter = new TreeMap<AmenityType, List<Amenity>>();
for (Amenity n : closestAmenities) {

View file

@ -14,6 +14,8 @@
</activity>
<activity android:name=".activities.SettingsActivity"
android:label="@string/settings_activity"></activity>
<activity android:name=".activities.SearchActivity"
android:label="@string/search_activity"></activity>
</application>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
OsmAnd/res/drawable/poi.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/list.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:stackFromBottom="true" android:transcriptMode="normal" />
</LinearLayout>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<ImageView android:id="@+id/icon" android:layout_width="20px"
android:paddingLeft="2px" android:paddingRight="2px"
android:paddingTop="2px" android:layout_height="fill_parent"/>
<TextView android:id="@+id/label" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="20px" />
</LinearLayout>

View file

@ -19,4 +19,6 @@
<string name="settings_Button">Settings</string>
<string name="search_button">Search</string>
<color name="menu_background">#CFFACD</color>
<string name="search_activity">search</string>
<color name="color_white">#FFFFFF</color>
</resources>

View file

@ -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){
@ -53,7 +56,35 @@ 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();
}
}

View file

@ -71,8 +71,6 @@ public class ResourceManager {
public AsyncLoadingThread asyncLoadingTiles = new AsyncLoadingThread();
public ResourceManager() {
// TODO start/stop this thread when needed?
asyncLoadingTiles.start();
@ -318,5 +316,4 @@ public class ResourceManager {
}
System.gc();
}
}

View file

@ -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

View file

@ -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;

View file

@ -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<String> amenityList = new ArrayList<String>();
Map<AmenityType, List<Amenity>> 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<Amenity> 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<Amenity> poiIndex = ResourceManager.getResourceManager().getPoiIndex();
filter = new TreeMap<AmenityType, List<Amenity>>();
LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(this);
List<Amenity> 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<Amenity>());
}
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));
}
}
}