Google Maps Android API v2 - restoring map state

ぐ巨炮叔叔 提交于 2019-12-12 11:40:29

问题


I'm building a very simple map app using Google Maps Android API v2. As expected, when the user leaves and then returns to the app, any changes they've made in location, zoom, etc. are lost as the activity is destroyed and re-created.

I know I can save the map's camera state programmatically (perhaps as values in shared preferences) in onPause() and restore it in onResume(), but does the API have any built-in mechanism for persisting map state between activity launches?


回答1:


I don't think you can, but you can save your CameraPosition which has your Position/Zoom/Angle...

http://developer.android.com/reference/com/google/android/gms/maps/model/CameraPosition.html

so you can write a function in your onDestroy which gets the CameraPosition from your map and store it in your SharedPreferences. In your onCreate() you recreate your CameraPosition from the SharedPreferences (after your map is instanciated).

// somewhere in your onDestroy()
@Override
protected void onDestroy() {
    CameraPosition mMyCam = MyMap.getCameraPosition();
    double longitude = mMyCam.target.longitude;
    (...)

    SharedPreferences settings = getSharedPreferences("SOME_NAME", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putDouble("longitude", longitude);
    (...) //put all other values like latitude, angle, zoom...
    editor.commit();
}

in your onCreate()

SharedPreferences settings = getSharedPreferences("SOME_NAME", 0);
// "initial longitude" is only used on first startup
double longitude = settings.getDouble("longitude", "initial_longitude");  
(...)  //add the other values

LatLng startPosition = new LatLng() //with longitude and latitude


CameraPosition cameraPosition = new CameraPosition.Builder()
.target(startPosition)      // Sets the center of the map to Mountain View
.zoom(17)                   // Sets the zoom
.bearing(90)                // Sets the orientation of the camera to east
.tilt(30)                   // Sets the tilt of the camera to 30 degrees
.build();                   // Creates a CameraPosition from the builder

create a new cameraPosition and animate it. be sure, map is instanziated at that point

 map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));



回答2:


As instructed here Retain instance of MapFragment, you can simply call setRetainInstance(true); for a MapFragment or SupportMapFragment in order to retain camera state and markers after a device orientation change. However, SharedPreferences is the way to go if you'd like to keep the state between app launches as mentioned by longilong




回答3:


There is nothing in the API for this use case.

You may want to try this library: https://github.com/fsilvestremorais/android-complex-preferences

It will not be as efficient as just writing a bunch of SharedPreferences.Editor.put... for values from CameraPosition in onPause, because it uses Gson internally, but saves some time if you have a more complex object to save and restore.

Anyway you may also post a feature request on gmaps-api-issues (or Android Maps Extensions). It's certainly something that is missing to persist simple objects (CameraPosition, LatLng, LatLngBounds) easily.



来源:https://stackoverflow.com/questions/16697891/google-maps-android-api-v2-restoring-map-state

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!