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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
74
vtm-android/src/org/oscim/android/MapPreferences.java
Normal file
74
vtm-android/src/org/oscim/android/MapPreferences.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user