From ee2de0c5cbe1d24b943afe9dc82f395509db68da Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 22 Jul 2010 07:00:43 +0000 Subject: [PATCH] implement layers git-svn-id: https://osmand.googlecode.com/svn/trunk@383 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/com/osmand/ToDoConstants.java | 3 +- OsmAnd/src/com/osmand/OsmandSettings.java | 5 ++ .../com/osmand/activities/MapActivity.java | 52 +++++++++++++----- .../osmand/activities/SettingsActivity.java | 55 ++++++++++--------- .../com/osmand/views/OsmandMapTileView.java | 25 +++++---- 5 files changed, 88 insertions(+), 52 deletions(-) diff --git a/DataExtractionOSM/src/com/osmand/ToDoConstants.java b/DataExtractionOSM/src/com/osmand/ToDoConstants.java index 46ad9f6c19..307b391120 100644 --- a/DataExtractionOSM/src/com/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/com/osmand/ToDoConstants.java @@ -11,7 +11,7 @@ public class ToDoConstants { // TODO ANDROID 0.3 // Improvements // 1. Show layers (?) - - // 0) map 1) transport (+) 2) Poi (choose filter) 3) osm bugs (+) 3) Favorites(+) + // 0) map 1) transport (+) 2) Poi (choose filter) (+) 3) osm bugs (+) 3) Favorites(+) // 4) Route (gmaps) - 5) Transport route - // and remove from settings (+) // 1.4 show detailed route on the map with turns and show route information directly (like in gmaps) @@ -19,6 +19,7 @@ public class ToDoConstants { // 2. Using NameFinder to search online - // 3. Show route info after route calc (+) // 4. show vehicle for calculating route (+) + // 5. Add zorders list to OsmandMapView // BUGS // ISSUE 21. (+, +, +) diff --git a/OsmAnd/src/com/osmand/OsmandSettings.java b/OsmAnd/src/com/osmand/OsmandSettings.java index 8c1719a1b8..9c300202e8 100644 --- a/OsmAnd/src/com/osmand/OsmandSettings.java +++ b/OsmAnd/src/com/osmand/OsmandSettings.java @@ -45,6 +45,11 @@ public class OsmandSettings { public static final int CENTER_CONSTANT = 0; public static final int BOTTOM_CONSTANT = 1; + + public static final Editor getWriteableEditor(Context ctx){ + SharedPreferences prefs = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); + return prefs.edit(); + } // this value string is synchronized with settings_pref.xml preference name public static final String USE_INTERNET_TO_DOWNLOAD_TILES = "use_internet_to_download_tiles"; //$NON-NLS-1$ diff --git a/OsmAnd/src/com/osmand/activities/MapActivity.java b/OsmAnd/src/com/osmand/activities/MapActivity.java index a139a7c5e2..69c25879fc 100644 --- a/OsmAnd/src/com/osmand/activities/MapActivity.java +++ b/OsmAnd/src/com/osmand/activities/MapActivity.java @@ -4,6 +4,7 @@ import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import android.app.Activity; import android.app.AlertDialog; @@ -195,7 +196,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso routingHelper = RoutingHelper.getInstance(this); // 1. route layer routeLayer = new RouteLayer(routingHelper); - mapView.addLayer(routeLayer); + mapView.addLayer(routeLayer, 1); // 2. osm bugs layer osmBugsLayer = new OsmBugsLayer(this); // 3. poi layer @@ -206,13 +207,13 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso transportStopsLayer = new TransportStopsLayer(); // 6. point navigation layer navigationLayer = new PointNavigationLayer(); - mapView.addLayer(navigationLayer); + mapView.addLayer(navigationLayer, 6); // 7. point location layer locationLayer = new PointLocationLayer(); - mapView.addLayer(locationLayer); + mapView.addLayer(locationLayer, 7); // 8. map info layer mapInfoLayer = new MapInfoLayer(this, routeLayer); - mapView.addLayer(mapInfoLayer); + mapView.addLayer(mapInfoLayer, 8); savingTrackHelper = new SavingTrackHelper(this); @@ -636,14 +637,14 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso private void updateLayers(){ if(mapView.getLayers().contains(transportStopsLayer) != OsmandSettings.isShowingTransportOverMap(this)){ if(OsmandSettings.isShowingTransportOverMap(this)){ - mapView.addLayer(transportStopsLayer, routeLayer); + mapView.addLayer(transportStopsLayer, 5); } else { mapView.removeLayer(transportStopsLayer); } } if(mapView.getLayers().contains(osmBugsLayer) != OsmandSettings.isShowingOsmBugs(this)){ if(OsmandSettings.isShowingOsmBugs(this)){ - mapView.addLayer(osmBugsLayer, routeLayer); + mapView.addLayer(osmBugsLayer, 2); } else { mapView.removeLayer(osmBugsLayer); } @@ -651,7 +652,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso if(mapView.getLayers().contains(poiMapLayer) != OsmandSettings.isShowingPoiOverMap(this)){ if(OsmandSettings.isShowingPoiOverMap(this)){ - mapView.addLayer(poiMapLayer, routeLayer); + mapView.addLayer(poiMapLayer, 3); } else { mapView.removeLayer(poiMapLayer); } @@ -659,7 +660,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso if(mapView.getLayers().contains(favoritesLayer) != OsmandSettings.isShowingFavorites(this)){ if(OsmandSettings.isShowingFavorites(this)){ - mapView.addLayer(favoritesLayer, routeLayer); + mapView.addLayer(favoritesLayer, 4); } else { mapView.removeLayer(favoritesLayer); } @@ -667,6 +668,14 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso } + private void updateMapSource(ITileSource newSource){ + if(mapView.getMap() instanceof SQLiteTileSource){ + ((SQLiteTileSource)mapView.getMap()).closeDB(); + } + ResourceManager.getResourceManager().setMapSource(newSource); + mapView.setMap(newSource); + } + @Override protected void onResume() { super.onResume(); @@ -679,11 +688,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso routingHelper = RoutingHelper.getInstance(this); ITileSource source = OsmandSettings.getMapTileSource(this); if(!Algoritms.objectEquals(mapView.getMap(), source)){ - if(mapView.getMap() instanceof SQLiteTileSource){ - ((SQLiteTileSource)mapView.getMap()).closeDB(); - } - ResourceManager.getResourceManager().setMapSource(source); - mapView.setMap(source); + updateMapSource(source); } updateApplicationModeSettings(); @@ -1097,7 +1102,8 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso @Override public void onClick(DialogInterface dialog, int item, boolean isChecked) { if (item == 0) { - // TODO show another builder with all sources + dialog.dismiss(); + selectMapLayer(); } else if(item == 1){ if(isChecked){ selectPOIFilterLayer(); @@ -1152,6 +1158,24 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso }); builder.show(); } + + private void selectMapLayer(){ + Map entriesMap = SettingsActivity.getTileSourceEntries(); + Builder builder = new AlertDialog.Builder(this); + final ArrayList keys = new ArrayList(entriesMap.keySet()); + builder.setItems(entriesMap.values().toArray(new String[entriesMap.size()]), new DialogInterface.OnClickListener(){ + @Override + public void onClick(DialogInterface dialog, int which) { + Editor edit = OsmandSettings.getWriteableEditor(MapActivity.this); + edit.putString(OsmandSettings.MAP_TILE_SOURCES, keys.get(which)); + edit.commit(); + updateMapSource(OsmandSettings.getMapTileSource(MapActivity.this)); + mapView.refreshMap(); + } + + }); + builder.show(); + } public void contextMenuPoint(final double latitude, final double longitude, boolean menu){ diff --git a/OsmAnd/src/com/osmand/activities/SettingsActivity.java b/OsmAnd/src/com/osmand/activities/SettingsActivity.java index 34f39d1db2..904837f31e 100644 --- a/OsmAnd/src/com/osmand/activities/SettingsActivity.java +++ b/OsmAnd/src/com/osmand/activities/SettingsActivity.java @@ -1,9 +1,10 @@ package com.osmand.activities; import java.io.File; -import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.Map; import java.util.Set; import android.app.ProgressDialog; @@ -302,32 +303,15 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference maxLevelToDownload.setValue(OsmandSettings.getMaximumLevelToDownloadTile(this)+""); //$NON-NLS-1$ - List list = TileSourceManager.getKnownSourceTemplates(); - List sqLiteFiles = new ArrayList(); - - File dir = new File(Environment.getExternalStorageDirectory(), ResourceManager.TILES_PATH); - if (dir != null) { - for (File f : dir.listFiles()) { - if (f.getName().endsWith(SQLiteTileSource.EXT)) { - sqLiteFiles.add(f); - } - } + Map entriesMap = getTileSourceEntries(); + entries = new String[entriesMap.size()]; + valueEntries = new String[entriesMap.size()]; + int ki = 0; + for(Map.Entry es : entriesMap.entrySet()){ + entries[ki] = es.getValue(); + valueEntries[ki] = es.getKey(); + ki++; } - - entries = new String[list.size() + sqLiteFiles.size()]; - valueEntries = new String[list.size() + sqLiteFiles.size()]; - for (int i = 0; i < list.size(); i++) { - entries[i + sqLiteFiles.size()] = list.get(i).getName(); - valueEntries[i + sqLiteFiles.size()] = list.get(i).getName(); - } - for (int i = 0; i < sqLiteFiles.size(); i++) { - String n = sqLiteFiles.get(i).getName(); - entries[i] = n.substring(0, n.indexOf('.')); - valueEntries[i] = sqLiteFiles.get(i).getName(); - } - - - tileSourcePreference.setEntries(entries); tileSourcePreference.setEntryValues(valueEntries); @@ -340,6 +324,25 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference tileSourcePreference.setSummary(summary + mapName); } + public static Map getTileSourceEntries(){ + + Map map = new LinkedHashMap(); + File dir = new File(Environment.getExternalStorageDirectory(), ResourceManager.TILES_PATH); + if (dir != null && dir.canRead()) { + for (File f : dir.listFiles()) { + if (f.getName().endsWith(SQLiteTileSource.EXT)) { + String n = f.getName(); + map.put(f.getName(), n.substring(0, n.indexOf('.'))); + } + } + } + for(TileSourceTemplate l : TileSourceManager.getKnownSourceTemplates()){ + map.put(l.getName(), l.getName()); + } + return map; + + } + @Override public boolean onPreferenceChange(Preference preference, Object newValue) { diff --git a/OsmAnd/src/com/osmand/views/OsmandMapTileView.java b/OsmAnd/src/com/osmand/views/OsmandMapTileView.java index 7efc23cef1..ef5a08f576 100644 --- a/OsmAnd/src/com/osmand/views/OsmandMapTileView.java +++ b/OsmAnd/src/com/osmand/views/OsmandMapTileView.java @@ -2,7 +2,9 @@ package com.osmand.views; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.apache.commons.logging.Log; @@ -89,6 +91,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall private OnTrackBallListener trackBallDelegate; private List layers = new ArrayList(); + private Map zOrders = new HashMap(); // UI Part // handler to refresh map (in ui thread - not necessary in ui thread, but msg queue is desirable). @@ -172,23 +175,23 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall public void surfaceDestroyed(SurfaceHolder holder) { } - public void addLayer(OsmandMapLayer layer, OsmandMapLayer afterIt){ - layer.initLayer(this); - int i = layers.indexOf(afterIt); - if(i == -1){ - layers.add(layer); - } else { - layers.add(i, layer); - } - } - public void addLayer(OsmandMapLayer layer){ + + public void addLayer(OsmandMapLayer layer, float zOrder){ + int i=0; + for(i=0; i zOrder){ + break; + } + } layer.initLayer(this); - layers.add(layer); + layers.add(i, layer); + zOrders.put(layer, zOrder); } public void removeLayer(OsmandMapLayer layer){ layers.remove(layer); + zOrders.remove(layer); layer.destroyLayer(); }