From 38a536457ab4332e30d9e539172989bd0451eb88 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 30 May 2011 00:07:06 +0200 Subject: [PATCH] Add overlay to map --- .../src/net/osmand/ToDoConstants.java | 1 - OsmAnd/res/values-ru/strings.xml | 4 + OsmAnd/res/values/strings.xml | 4 + OsmAnd/res/xml/settings_pref.xml | 1 + .../src/net/osmand/plus/OsmandSettings.java | 23 +++--- .../plus/activities/MapActivityLayers.java | 75 +++++++++++++++++-- .../plus/activities/SettingsActivity.java | 50 +++++++++---- .../net/osmand/plus/views/MapTileLayer.java | 1 - 8 files changed, 125 insertions(+), 34 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/ToDoConstants.java b/DataExtractionOSM/src/net/osmand/ToDoConstants.java index 7904494453..b1234b6f08 100644 --- a/DataExtractionOSM/src/net/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/net/osmand/ToDoConstants.java @@ -12,7 +12,6 @@ public class ToDoConstants { // Map Refactoring // Remove notification from OsmAndMapTileView (?) -// Add transparency filter to bitmap layers // Allow to create over/under map layers // Allow to config transparency filter // Yandex Traffic make downloadable diff --git a/OsmAnd/res/values-ru/strings.xml b/OsmAnd/res/values-ru/strings.xml index 3fb6f5408a..4407ee054d 100644 --- a/OsmAnd/res/values-ru/strings.xml +++ b/OsmAnd/res/values-ru/strings.xml @@ -1,5 +1,9 @@ + Карта покрытия... + Нету + Карта покрытия + Выберите слой для покрытия основной карты Векторные карты Использовать загруженные векторные карты Карта уже установлена, настройки будут обновлены diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 11c34018d3..674c17eee2 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1,5 +1,9 @@ + Overlay map... + None + Overlay map + Choose overlay map Vector maps Use downloaded vector maps Map is already installed, settings will be updated diff --git a/OsmAnd/res/xml/settings_pref.xml b/OsmAnd/res/xml/settings_pref.xml index bd29e2f9fd..40dda13af9 100644 --- a/OsmAnd/res/xml/settings_pref.xml +++ b/OsmAnd/res/xml/settings_pref.xml @@ -11,6 +11,7 @@ android:key="use_english_names"> + diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index ba4b905372..47c4b42c3f 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -69,7 +69,6 @@ public class OsmandSettings { private List internetAvailableSourceTemplates = null; // TODO make all layers profile preferenced???? - // TODO profile preferences for map is using vector map??? private OsmandSettings(OsmandApplication ctx){ this.ctx = ctx; globalPreferences = ctx.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE); @@ -514,16 +513,12 @@ public class OsmandSettings { false, true); // this value string is synchronized with settings_pref.xml preference name - public static final String MAP_TILE_SOURCES = "map_tile_sources"; //$NON-NLS-1$ + public final CommonPreference MAP_OVERLAY = new StringPreference("map_overlay", + null, true); - - public boolean setMapTileSource(String tileSource){ - return globalPreferences.edit().putString(MAP_TILE_SOURCES, tileSource).commit(); - } - - public String getMapTileSourceName(){ - return globalPreferences.getString(MAP_TILE_SOURCES, TileSourceManager.getMapnikSource().getName()); - } + // this value string is synchronized with settings_pref.xml preference name + public final CommonPreference MAP_TILE_SOURCES = new StringPreference("map_tile_sources", + TileSourceManager.getMapnikSource().getName(), true); public List getInternetAvailableSourceTemplates(){ if(internetAvailableSourceTemplates == null && isInternetConnectionAvailable()){ @@ -533,7 +528,7 @@ public class OsmandSettings { } public ITileSource getMapTileSource() { - String tileName = globalPreferences.getString(MAP_TILE_SOURCES, null); + String tileName = MAP_TILE_SOURCES.get(); if (tileName != null) { ITileSource ts = getTileSourceByName(tileName); if(ts != null){ @@ -542,8 +537,12 @@ public class OsmandSettings { } return TileSourceManager.getMapnikSource(); } + - private ITileSource getTileSourceByName(String tileName) { + public ITileSource getTileSourceByName(String tileName) { + if(tileName == null || tileName.length() == 0){ + return null; + } List list = TileSourceManager.getKnownSourceTemplates(); File tPath = extendOsmandPath(ResourceManager.TILES_PATH); File dir = new File(tPath, tileName); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index 9534cb0837..01446fb7db 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -15,8 +15,6 @@ import net.osmand.GPXUtilities.GPXFileResult; import net.osmand.GPXUtilities.WptPt; import net.osmand.data.AmenityType; import net.osmand.map.ITileSource; -import net.osmand.map.TileSourceManager; -import net.osmand.map.TileSourceManager.TileSourceTemplate; import net.osmand.osm.LatLon; import net.osmand.plus.OsmandSettings; import net.osmand.plus.PoiFilter; @@ -61,6 +59,7 @@ public class MapActivityLayers { // the order of layer should be preserved ! when you are inserting new layer private MapTileLayer mapTileLayer; private MapVectorLayer mapVectorLayer; + private MapTileLayer overlayLayer; private GPXLayer gpxLayer; private RouteLayer routeLayer; private YandexTrafficLayer trafficLayer; @@ -97,9 +96,12 @@ public class MapActivityLayers { mapVectorLayer = new MapVectorLayer(mapTileLayer); mapView.addLayer(mapVectorLayer, 0.5f); - // 0.6 gpx layer + overlayLayer = new MapTileLayer(); + // mapView.addLayer(overlayLayer, 0.7f); + + // 0.9 gpx layer gpxLayer = new GPXLayer(); - mapView.addLayer(gpxLayer, 0.6f); + mapView.addLayer(gpxLayer, 0.9f); // 1. route layer routeLayer = new RouteLayer(routingHelper); @@ -109,7 +111,6 @@ public class MapActivityLayers { trafficLayer = new YandexTrafficLayer(); mapView.addLayer(trafficLayer, 1.5f); - // 2. osm bugs layer osmBugsLayer = new OsmBugsLayer(activity); // 3. poi layer @@ -182,6 +183,9 @@ public class MapActivityLayers { public void updateMapSource(OsmandMapTileView mapView){ OsmandSettings settings = getApplication().getSettings(); boolean showTiles = !settings.MAP_VECTOR_DATA.get(); + // update overlay layer + updateOverlay(mapView, settings); + ITileSource source = showTiles ? settings.getMapTileSource() : null; if (showTiles == mapTileLayer.isVisible() && Algoritms.objectEquals(mapTileLayer.getMap(), source)) { return; @@ -209,6 +213,22 @@ public class MapActivityLayers { } else { mapView.setMainLayer(mapTileLayer); } + + + + } + + private void updateOverlay(OsmandMapTileView mapView, OsmandSettings settings) { + ITileSource overlay = settings.getTileSourceByName(settings.MAP_OVERLAY.get()); + if(!Algoritms.objectEquals(overlay, overlayLayer.getMap())){ + if(overlay == null){ + mapView.removeLayer(overlayLayer); + } else { + mapView.addLayer(overlayLayer, 0.7f); + } + overlayLayer.setMap(overlay); + mapView.refreshMap(); + } } public void openLayerSelectionDialog(final OsmandMapTileView mapView){ @@ -220,6 +240,7 @@ public class MapActivityLayers { layersList.add(getString(R.string.layer_osm_bugs)); layersList.add(getString(R.string.layer_favorites)); layersList.add(getString(R.string.layer_gpx_layer)); + layersList.add(getString(R.string.layer_overlay)); final int routeInfoInd = routeInfoLayer.couldBeVisible() ? layersList.size() : -1; if(routeInfoLayer.couldBeVisible()){ layersList.add(getString(R.string.layer_route)); @@ -238,6 +259,7 @@ public class MapActivityLayers { selected[3] = settings.SHOW_OSM_BUGS.get(); selected[4] = settings.SHOW_FAVORITES.get(); selected[5] = gpxLayer.isVisible(); + selected[6] = overlayLayer.getMap() != null; selected[trafficInd] = trafficLayer.isVisible(); if(routeInfoInd != -1){ selected[routeInfoInd] = routeInfoLayer.isUserDefinedVisible(); @@ -273,6 +295,14 @@ public class MapActivityLayers { dialog.dismiss(); useGPXFileLayer(false, null, mapView); } + } else if(item == 6){ + if(overlayLayer.getMap() != null){ + settings.MAP_OVERLAY.set(null); + updateMapSource(mapView); + } else { + dialog.dismiss(); + selectMapOverlayLayer(mapView); + } } else if(item == routeInfoInd){ routeInfoLayer.setVisible(isChecked); } else if(item == transportRouteInfoInd){ @@ -451,7 +481,7 @@ public class MapActivityLayers { } }); } else { - settings.setMapTileSource(keys.get(which - 1)); + settings.MAP_TILE_SOURCES.set(keys.get(which - 1)); settings.MAP_VECTOR_DATA.set(false); updateMapSource(mapView); } @@ -463,6 +493,39 @@ public class MapActivityLayers { } + private void selectMapOverlayLayer(final OsmandMapTileView mapView){ + final OsmandSettings settings = getApplication().getSettings(); + Map entriesMap = settings.getTileSourceEntries(); + final ArrayList keys = new ArrayList(entriesMap.keySet()); + Builder builder = new AlertDialog.Builder(activity); + final String[] items = new String[entriesMap.size() + 1]; + int i = 0; + for(String it : entriesMap.values()){ + items[i++] = it; + } + + items[i] = getString(R.string.more_external_layer); + builder.setItems(items, new DialogInterface.OnClickListener(){ + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == items.length - 1){ + SettingsActivity.installMapLayers(activity, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + openLayerSelectionDialog(mapView); + } + }); + } else { + settings.MAP_OVERLAY.set(keys.get(which)); + updateMapSource(mapView); + } + + } + + }); + builder.show(); + } + private String getString(int resId) { return activity.getString(resId); diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java index c96242914c..2e8206d386 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsActivity.java @@ -55,6 +55,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference private EditTextPreference applicationDir; private ListPreference tileSourcePreference; + private ListPreference overlayPreference; private CheckBoxPreference routeServiceEnabled; private BroadcastReceiver broadcastReceiver; @@ -249,8 +250,10 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference entries = (String[]) rendererNames.toArray(new String[rendererNames.size()]); registerListPreference(osmandSettings.RENDERER, screen, entries, entries); - tileSourcePreference = (ListPreference) screen.findPreference(OsmandSettings.MAP_TILE_SOURCES); + tileSourcePreference = (ListPreference) screen.findPreference(osmandSettings.MAP_TILE_SOURCES.getId()); tileSourcePreference.setOnPreferenceChangeListener(this); + overlayPreference = (ListPreference) screen.findPreference(osmandSettings.MAP_OVERLAY.getId()); + overlayPreference.setOnPreferenceChangeListener(this); reloadIndexes =(Preference) screen.findPreference(OsmandSettings.RELOAD_INDEXES); @@ -331,10 +334,32 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference } private void updateTileSourceSummary() { + fillTileSourcesToPreference(tileSourcePreference, osmandSettings.MAP_TILE_SOURCES.get(), false); + fillTileSourcesToPreference(overlayPreference, osmandSettings.MAP_OVERLAY.get(), true); + + String mapName = " " + osmandSettings.MAP_TILE_SOURCES.get(); //$NON-NLS-1$ + String summary = tileSourcePreference.getSummary().toString(); + if (summary.lastIndexOf(':') != -1) { + summary = summary.substring(0, summary.lastIndexOf(':') + 1); + } + tileSourcePreference.setSummary(summary + mapName); + } + + private void fillTileSourcesToPreference(ListPreference tileSourcePreference, String value, boolean addNone) { Map entriesMap = osmandSettings.getTileSourceEntries(); - String[] entries = new String[entriesMap.size() + 1]; - String[] values = new String[entriesMap.size() + 1]; + int add = addNone ? 1 : 0; + String[] entries = new String[entriesMap.size() + 1 + add]; + String[] values = new String[entriesMap.size() + 1 + add]; int ki = 0; + if(addNone){ + entries[ki] = getString(R.string.default_none); + values[ki] = ""; + ki++; + } + if (value == null) { + value = ""; + } + for(Map.Entry es : entriesMap.entrySet()){ entries[ki] = es.getValue(); values[ki] = es.getKey(); @@ -342,15 +367,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference } entries[ki] = getString(R.string.more_external_layer); values[ki] = MORE_VALUE; - String value = osmandSettings.getMapTileSourceName(); fill(tileSourcePreference, entries, values, value); - - String mapName = " " + osmandSettings.getMapTileSourceName(); //$NON-NLS-1$ - String summary = tileSourcePreference.getSummary().toString(); - if (summary.lastIndexOf(':') != -1) { - summary = summary.substring(0, summary.lastIndexOf(':') + 1); - } - tileSourcePreference.setSummary(summary + mapName); } private void fill(ListPreference component, String[] list, String[] values, String selected) { @@ -421,7 +438,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference routeServiceEnabled.setChecked(getMyApplication().getNavigationService() != null); } } - } else if (preference == tileSourcePreference) { + } else if (preference == tileSourcePreference || preference == overlayPreference) { if(MORE_VALUE.equals(newValue)){ SettingsActivity.installMapLayers(this, new DialogInterface.OnClickListener() { @Override @@ -429,9 +446,14 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference updateTileSourceSummary(); } }); - } else { - osmandSettings.setMapTileSource((String) newValue); + } else if(preference == tileSourcePreference){ + osmandSettings.MAP_TILE_SOURCES.set((String) newValue); updateTileSourceSummary(); + } else { + if(((String) newValue).length() == 0){ + newValue = null; + } + osmandSettings.MAP_OVERLAY.set(((String) newValue)); } } return true; diff --git a/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java b/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java index e46f315968..1e8f338fd5 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java @@ -1,7 +1,6 @@ package net.osmand.plus.views; import net.osmand.map.ITileSource; -import net.osmand.osm.MapUtils; import net.osmand.plus.OsmandSettings; import net.osmand.plus.ResourceManager; import android.graphics.Bitmap;