7

Google Maps Android API v2 を使用して非常に単純な地図アプリを構築しています。予想どおり、ユーザーがアプリを離れてからアプリに戻ると、アクティビティが破棄されて再作成されるため、場所、ズームなどで行った変更はすべて失われます。

マップのカメラの状態をプログラムで (おそらく共有設定の値として) に保存しonPause()て復元できることはわかっonResume()ていますが、API には、アクティビティの起動間でマップの状態を保持するための組み込みメカニズムがありますか?

4

4 に答える 4

9

できないと思いますが、CameraPosition位置/ズーム/角度を持つものを保存できます...

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

onDestroyそのため、マップからを取得してCameraPositionに格納する関数を に記述できますSharedPreferences。あなたは(マップがインスタンス化された後)からあなたonCreate()を再作成します。CameraPositionSharedPreferences

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

あなたの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

新しい cameraPosition を作成してアニメーション化します。確かに、マップはその時点でインスタンス化されています

 map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
于 2013-05-22T18:00:00.650 に答える
0

このユース ケースの API には何もありません。

このライブラリを試してみてください: https://github.com/fsilvestremorais/android-complex-preferences

内部で Gson を使用するため、 inSharedPreferences.Editor.put...から一連の for 値を書き込むほど効率的ではありませんが、保存および復元するオブジェクトがより複雑な場合は、時間を節約できます。CameraPositiononPause

とにかく、gmaps-api-issues (またはAndroid Maps Extensions ) に機能リクエストを投稿することもできます。CameraPosition単純なオブジェクト ( 、LatLngLatLngBounds) を簡単に永続化するには、確かに欠けているものです。

于 2013-05-22T20:45:30.530 に答える