我想在我正在开发的android应用程序中保存谷歌地图的状态,我设法保存相机的状态,但包括标记的状态没有成功。有什么建议??
我创建了一个保存相机状态的函数以及我创建的用于尝试保存书签类的方法。但它不起作用。
public class MapStateManager {
private SharedPreferences mapStatePrefs;
private MarkerOptions markerOptions;
private GoogleMap map;
private static final String LATITUD = "latitud";
private static final String LONGITUD = "longitud";
private static final String LATITUD_MARKER = "latitud";
private static final String LONGITUD_MARKER = "longitud";
private static final String ZOOM = "zoom";
private static final String BEARING = "bearing";
private static final String TILT = "tilt";
private static final String MAPTYPE = "maptype";
private static final String PREFS_NAME = "mapCameraState";
public MapStateManager(Context context, MarkerOptions markerOptions){
this.markerOptions = markerOptions;
mapStatePrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
}
public void saveMapState(GoogleMap map){
SharedPreferences.Editor editor = mapStatePrefs.edit();
CameraPosition position = map.getCameraPosition();
editor.putFloat(LATITUD, (float) position.target.latitude);
editor.putFloat(LONGITUD, (float) position.target.longitude);
editor.putFloat(ZOOM, position.zoom);
editor.putFloat(TILT, position.tilt);
editor.putFloat(BEARING,position.bearing);
editor.putFloat(MAPTYPE, map.getMapType());
editor.commit();
}
public MarkerOptions saveMarkersState(){
MarkerOptions marker = markerOptions;
marker = new MarkerOptions()
.title("marker")
.position(new LatLng(marker.getPosition().latitude, marker.getPosition().longitude))
.icon(BitmapDescriptorFactory.defaultMarker());
map.addMarker(marker);
if(marker.getPosition().latitude == 0){
return null;
}
if(marker.getPosition().longitude == 0){
return null;
}
//LatLng position = new LatLng(latitud, longitud);
SharedPreferences.Editor editor = mapStatePrefs.edit();
editor.putFloat(LATITUD_MARKER, (float) marker.getPosition().latitude);
editor.putFloat(LONGITUD_MARKER, (float) marker.getPosition().longitude);
editor.commit();
return marker;
}
public CameraPosition saveCameraPosition(){
double latitud = mapStatePrefs.getFloat(LATITUD, 0);
if(latitud == 0){
return null;
}
double longitud= mapStatePrefs.getFloat(LONGITUD, 0);
LatLng target = new LatLng(latitud, longitud);
float zoom = mapStatePrefs.getFloat(ZOOM, 0);
float bearing = mapStatePrefs.getFloat(BEARING, 0);
float tilt = mapStatePrefs.getFloat(TILT, 0);
CameraPosition position = new CameraPosition(target, zoom, tilt, bearing);
return position;
}
}
@Override
protected void onResume() {
super.onResume();
mapFragment.onResume();
// onSaveInstanceState(state);
setUpMapIfNeeded();
MapStateManager mapstateCamera = new MapStateManager(this, markerOptions);
CameraPosition position = mapstateCamera.saveCameraPosition();
if(position != null){
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
mMap.moveCamera(update);
//mMap.getMyLocation();
}
/*if(markerOptions != null){
mapstateCamera.saveMarkersState();
}*/
//
}
@Override
protected void onSaveInstanceState(Bundle guardarEstado) {
super.onSaveInstanceState(guardarEstado);
String joined = TextUtils.join(", ", markers);
guardarEstado.putString(STATE_MAP_MARKER, joined);
}
@Override
protected void onRestoreInstanceState(Bundle recuperarEstado) {
super.onRestoreInstanceState(recuperarEstado);
mCurrentScore = recuperarEstado.getString(STATE_MAP_RECOVERY);
}
@Override
protected void onStop() {
MapStateManager mapState = new MapStateManager(this, markerOptions);
mapState.saveMapState(mMap);
//mapState.saveMarkersState();
super.onStop();
}
@Override
protected void onPause() {
super.onPause();
mapFragment.onPause();
}
感谢所有人:)
答案 0 :(得分:0)
这个问题在过去的SO上已经提出了很多时间。
处理这种情况的最佳方法是使用共享首选项并在本地存储标记。即使应用程序被杀死,重新打开时标记也会恢复到同一位置。
有关代码,请参阅此Stack Overflow应答。