android: remove MapActivity

- see vtm-android-start for an example use of MapView
- add MapPreferences

- update android-start
- android-example: use appcompat actionbar
This commit is contained in:
Hannes Janetzek 2014-10-20 05:14:00 +02:00
parent 0c5a7e7aa6
commit b1cfdfd454
15 changed files with 197 additions and 187 deletions

View File

@ -15,8 +15,8 @@
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<!-- android:theme="@style/AppTheme" -->
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="org.oscim.android.test.Samples"
android:label="@string/app_name" >

View File

@ -26,7 +26,7 @@
android:id="@+id/controls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#cc000000"
android:background="#ccdddddd"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"

View File

@ -1,5 +1,5 @@
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar" />
<style name="AppTheme" parent="Theme.AppCompat.Light" />
</resources>

View File

@ -16,8 +16,6 @@
*/
package org.oscim.android.test;
import org.oscim.android.MapActivity;
import org.oscim.android.MapView;
import org.oscim.android.cache.TileCache;
import org.oscim.core.MapPosition;
import org.oscim.layers.TileGridLayer;
@ -37,30 +35,22 @@ public class BaseMapActivity extends MapActivity {
final static boolean USE_CACHE = true;
MapView mMapView;
VectorTileLayer mBaseLayer;
TileSource mTileSource;
TileGridLayer mGridLayer;
private TileCache mCache;
protected final int mContentView;
public BaseMapActivity(int contentView) {
mContentView = contentView;
super(contentView);
}
public BaseMapActivity() {
this(R.layout.activity_map);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(mContentView);
mMapView = (MapView) findViewById(R.id.mapView);
registerMapView(mMapView);
mTileSource = new OSciMap4TileSource();
@ -76,7 +66,6 @@ public class BaseMapActivity extends MapActivity {
mMap.getMapPosition(pos);
if (pos.x == 0.5 && pos.y == 0.5)
mMap.setMapPosition(53.08, 8.83, Math.pow(2, 16));
}
@Override
@ -88,7 +77,7 @@ public class BaseMapActivity extends MapActivity {
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.theme_default:

View File

@ -14,8 +14,6 @@
*/
package org.oscim.android.test;
import org.oscim.android.MapActivity;
import org.oscim.android.MapView;
import org.oscim.android.cache.TileCache;
import org.oscim.backend.canvas.Color;
import org.oscim.core.MapPosition;
@ -23,44 +21,51 @@ import org.oscim.core.MercatorProjection;
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
import org.oscim.renderer.MapRenderer;
import org.oscim.tiling.TileSource;
import org.oscim.tiling.source.bitmap.BitmapTileSource;
import org.oscim.tiling.source.bitmap.DefaultSources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.os.Bundle;
public class BitmapTileMapActivity extends MapActivity {
static final Logger log = LoggerFactory.getLogger(BitmapTileMapActivity.class);
private final static boolean USE_CACHE = true;
private final TileSource mTileSource;
private final BitmapTileSource mTileSource;
protected BitmapTileLayer mBitmapLayer;
public BitmapTileMapActivity() {
//mTileSource = DefaultSources.STAMEN_TONER.build();
mTileSource = DefaultSources.OPENSTREETMAP.build();
this(DefaultSources.OPENSTREETMAP.build());
}
public BitmapTileMapActivity(TileSource tileSource) {
public BitmapTileMapActivity(BitmapTileSource tileSource) {
super(R.layout.activity_map);
mTileSource = tileSource;
}
MapView mMapView;
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mMapView = (MapView) findViewById(R.id.mapView);
registerMapView(mMapView);
MapRenderer.setBackgroundColor(0xff777777);
mMap.layers().add(new TileGridLayer(mMap, Color.GRAY, 1.8f, 8));
if (mTileSource == null)
return;
if (USE_CACHE) {
mCache = new TileCache(this, null, mTileSource.getClass().getSimpleName());
String cacheFile = mTileSource.getUrl()
.toString()
.replaceFirst("https?://", "")
.replaceAll("/", "-");
log.debug("use bitmap cache {}", cacheFile);
mCache = new TileCache(this, null, cacheFile);
mCache.setCacheSize(512 * (1 << 10));
mTileSource.setCache(mCache);
}
@ -74,7 +79,7 @@ public class BitmapTileMapActivity extends MapActivity {
@Override
protected void onDestroy() {
super.onDestroy();
if (USE_CACHE)
if (mCache != null)
mCache.dispose();
}

View File

@ -0,0 +1,53 @@
package org.oscim.android.test;
import org.oscim.android.MapPreferences;
import org.oscim.android.MapView;
import org.oscim.map.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public abstract class MapActivity extends ActionBarActivity {
public static final Logger log = LoggerFactory.getLogger(MapActivity.class);
MapView mMapView;
Map mMap;
MapPreferences mPrefs;
protected final int mContentView;
public MapActivity(int contentView) {
mContentView = contentView;
}
public MapActivity() {
this(R.layout.activity_map);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(mContentView);
mMapView = (MapView) findViewById(R.id.mapView);
mMap = mMapView.map();
mPrefs = new MapPreferences(MapActivity.class.getName(), this);
}
@Override
protected void onResume() {
super.onResume();
mPrefs.load(mMapView.map());
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
mPrefs.save(mMapView.map());
}
}

View File

@ -14,7 +14,6 @@
*/
package org.oscim.android.test;
import org.oscim.android.MapActivity;
import org.oscim.android.MapView;
import org.oscim.android.filepicker.FilePicker;
import org.oscim.android.filepicker.FilterByFileExtension;
@ -42,10 +41,6 @@ public class MapsforgeMapActivity extends MapActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mMapView = (MapView) findViewById(R.id.mapView);
registerMapView(mMapView);
startActivityForResult(new Intent(this, MapFilePicker.class),
SELECT_MAP_FILE);
@ -65,7 +60,7 @@ public class MapsforgeMapActivity extends MapActivity {
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.theme_default:

View File

@ -84,7 +84,14 @@ public class MarkerOverlayActivity extends BitmapTileMapActivity
markerLayer.addItems(pts);
mMap.layers().add(new TileGridLayer(mMap));
mMap.setMapPosition(0, 0, 1);
}
@Override
protected void onResume() {
super.onResume();
/* ignore saved position */
mMap.setMapPosition(0, 0, 1 << 2);
mMapView.onResume();
}
@Override

View File

@ -16,7 +16,6 @@ package org.oscim.android.test;
import static org.oscim.tiling.source.bitmap.DefaultSources.STAMEN_TONER;
import org.oscim.android.MapActivity;
import org.oscim.android.MapView;
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
@ -43,10 +42,6 @@ public class OsmJsonMapActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mMapView = (MapView) findViewById(R.id.mapView);
registerMapView(mMapView);
mTileSource = new OsmWaterJsonTileSource();

View File

@ -41,11 +41,17 @@ public class PathOverlayActivity extends BitmapTileMapActivity {
createLayers(1, true);
mMap.setMapPosition(0, 0, 1 << 2);
looooop();
}
@Override
protected void onResume() {
super.onResume();
/* ignore saved position */
mMap.setMapPosition(0, 0, 1 << 2);
mMapView.onResume();
}
void looooop() {
mMap.postDelayed(new Runnable() {
@Override

View File

@ -21,7 +21,6 @@ import org.oscim.core.MercatorProjection;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
import org.oscim.map.Layers;
import org.oscim.map.Map;
import org.oscim.theme.IRenderTheme;
import org.oscim.theme.ThemeLoader;
import org.oscim.theme.VtmThemes;
@ -33,14 +32,13 @@ public class SimpleMapActivity extends BaseMapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Map m = this.map();
Layers layers = mMap.layers();
layers.add(new BuildingLayer(mMap, mBaseLayer));
layers.add(new LabelLayer(mMap, mBaseLayer));
layers.add(new MapScaleBar(mMapView));
m.setTheme(VtmThemes.DEFAULT);
mMap.setTheme(VtmThemes.DEFAULT);
}
void runTheMonkey() {

View File

@ -1,6 +1,7 @@
package org.oscim.android.start;
import org.oscim.android.MapActivity;
import org.oscim.android.MapPreferences;
import org.oscim.android.MapView;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
@ -11,24 +12,41 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class TestActivity extends MapActivity {
public class TestActivity extends ActionBarActivity {
public static final Logger log = LoggerFactory.getLogger(TestActivity.class);
MapView mMapView;
MapPreferences mPrefs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Map map = this.map();
mMapView = (MapView) findViewById(R.id.mapView);
Map map = mMapView.map();
mPrefs = new MapPreferences(TestActivity.class.getName(), this);
VectorTileLayer baseLayer = map.setBaseMap(new OSciMap4TileSource());
map.layers().add(new BuildingLayer(map, baseLayer));
map.layers().add(new LabelLayer(map, baseLayer));
map.setTheme(VtmThemes.DEFAULT);
//mMap.setMapPosition(49.417, 8.673, 1 << 17);
map.setMapPosition(53.5620092, 9.9866457, 1 << 16);
// mMap.layers().add(new TileGridLayer(mMap));
}
@Override
protected void onResume() {
super.onResume();
mPrefs.load(mMapView.map());
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
mPrefs.save(mMapView.map());
}
}

View File

@ -1,125 +0,0 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013 Hannes Janetzek
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.android;
import org.oscim.core.GeoPoint;
import org.oscim.core.MapPosition;
import org.oscim.map.Map;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.app.FragmentActivity;
/**
* MapActivity is the abstract base class which can be extended in order to use
* a {@link MapView}. There are no abstract methods in this implementation which
* subclasses need to override and no API key or registration is required.
* <p>
* A subclass may create a MapView either via one of the MapView constructors or
* by inflating an XML layout file.
* <p>
* When the MapActivity is shut down, the current center position, zoom level
* and map file of the MapView are saved in a preferences file and restored in
* the next startup process.
*/
public abstract class MapActivity extends FragmentActivity {
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
private static final String KEY_MAP_SCALE = "map_scale";
private static final String PREFERENCES_FILE = "MapActivity";
private static boolean containsViewport(SharedPreferences sharedPreferences) {
return sharedPreferences.contains(KEY_LATITUDE)
&& sharedPreferences.contains(KEY_LONGITUDE)
&& sharedPreferences.contains(KEY_MAP_SCALE);
}
protected Map mMap;
protected MapView mMapView;
public Map map() {
return mMap;
}
@Override
protected void onDestroy() {
super.onDestroy();
mMap.destroy();
}
@Override
protected void onPause() {
super.onPause();
Editor editor = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE).edit();
editor.clear();
// save the map position
MapPosition mapPosition = new MapPosition();
mMap.viewport().getMapPosition(mapPosition);
GeoPoint geoPoint = mapPosition.getGeoPoint();
editor.putInt(KEY_LATITUDE, geoPoint.latitudeE6);
editor.putInt(KEY_LONGITUDE, geoPoint.longitudeE6);
editor.putFloat(KEY_MAP_SCALE, (float) mapPosition.scale);
editor.commit();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onStop() {
super.onStop();
mMapView.onPause();
}
/**
* This method is called once by each MapView during its setup process.
*
* @param mapView
* the calling MapView.
*/
public final void registerMapView(MapView mapView) {
mMapView = mapView;
mMap = mapView.map();
SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES_FILE,
MODE_PRIVATE);
if (containsViewport(sharedPreferences)) {
// get and set the map position and zoom level
int latitudeE6 = sharedPreferences.getInt(KEY_LATITUDE, 0);
int longitudeE6 = sharedPreferences.getInt(KEY_LONGITUDE, 0);
float scale = sharedPreferences.getFloat(KEY_MAP_SCALE, 1);
MapPosition mapPosition = new MapPosition();
mapPosition.setPosition(latitudeE6 / 1E6, longitudeE6 / 1E6);
mapPosition.setScale(scale);
mMap.setMapPosition(mapPosition);
}
}
}

View File

@ -0,0 +1,74 @@
package org.oscim.android;
import org.oscim.core.MapPosition;
import org.oscim.map.Map;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class MapPreferences {
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
private static final String KEY_SCALE = "scale";
private final String PREFERENCES_FILE;
Context ctx;
public MapPreferences(String name, Context ctx) {
this.ctx = ctx;
this.PREFERENCES_FILE = name;
}
private void putDouble(Editor editor, String key, double value) {
editor.putLong(key, Double.doubleToLongBits(value));
}
private double getDouble(SharedPreferences prefs, String key) {
return Double.longBitsToDouble(prefs.getLong(key, 0));
}
public void save(Map map) {
save(map.getMapPosition());
}
public void save(MapPosition pos) {
Editor editor = ctx.getSharedPreferences(PREFERENCES_FILE,
Activity.MODE_PRIVATE).edit();
editor.clear();
putDouble(editor, KEY_LATITUDE, pos.y);
putDouble(editor, KEY_LONGITUDE, pos.x);
putDouble(editor, KEY_SCALE, pos.scale);
putDouble(editor, KEY_LATITUDE, pos.y);
editor.commit();
}
private static boolean containsViewport(SharedPreferences prefs) {
return prefs.contains(KEY_LATITUDE)
&& prefs.contains(KEY_LONGITUDE)
&& prefs.contains(KEY_SCALE);
}
public boolean load(Map map) {
MapPosition pos = map.getMapPosition();
if (load(pos)) {
map.setMapPosition(pos);
return true;
}
return false;
}
public boolean load(MapPosition pos) {
SharedPreferences prefs = ctx.getSharedPreferences(PREFERENCES_FILE,
Activity.MODE_PRIVATE);
if (containsViewport(prefs)) {
pos.x = getDouble(prefs, KEY_LONGITUDE);
pos.y = getDouble(prefs, KEY_LATITUDE);
pos.scale = getDouble(prefs, KEY_SCALE);
return true;
}
return false;
}
}

View File

@ -60,7 +60,6 @@ public class MapView extends GLSurfaceView {
this(context, null);
}
@SuppressWarnings("deprecation")
public MapView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
@ -92,10 +91,6 @@ public class MapView extends GLSurfaceView {
setRenderer(new GLRenderer(mMap));
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
/* to be removed */
if (context instanceof MapActivity)
((MapActivity) context).registerMapView(this);
mMap.clearMap();
mMap.updateMap(false);