0

I would like to save the state of google maps in android application I'm developing, I managed to save the state of the camera, but the state including the markers have not succeeded. Any suggestions ??

I have created a function to save the state of the camera and a method that I created to try to save bookmarks class. But it does not work.

MapStateManager.java

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;


}
}

MainActivity.java (Override methods)

@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();
}

Thanks to all :)

Vishal Raj
  • 1,755
  • 2
  • 16
  • 35
eVolaXx
  • 1
  • 4

1 Answers1

0

This question has been raised a lot of time in the past on SO.

The best way to deal with this situation is to use Shared Preference and store the markers locally. Even if the app is killed the markers would be resumed to the same location when reopened.

For code, refer to this Stack Overflow answer.

How to save a marker onMapClick?

Community
  • 1
  • 1
AniV
  • 3,997
  • 1
  • 12
  • 17