画面回転やアクティティが殺される問題をicepickでサクッと解決する

![Screenshot_20161208-121844_png.png](https://qiita-image-store.s3.amazonaws.com/0/99731/4d3d0327-1982-1a95-5907-8f56ad0756fc.png)


別のアクティビティから地図の画面に戻ってきたり、画面回転すると、現在位置が変わって使い勝手が悪いというレビューを見ます。icepickで簡単に解決できます。

## Google Maps Activityを選択

![Create_New_Project.png](https://qiita-image-store.s3.amazonaws.com/0/99731/40abc08d-1f3e-dec4-1a61-343cc3c916e1.png)


## キーの設定

![google_maps_api_xml_-_Map_-____Desktop_Android_Dropbox_Map_.png](https://qiita-image-store.s3.amazonaws.com/0/99731/5af0125b-18f2-8775-4483-d147a3b6cf8d.png)

## Google developers consoleでGoogle Maps Android APIのキーを取得

https://console.developers.google.com/?hl=JA

## icepickインストール
LatLngとzoomの値を保持するために必要なライブラリ。

```py3

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "jp.co.demo.map"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        //追加
        repositories {
            maven {url "https://clojars.org/repo/"}
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.google.android.gms:play-services:9.6.1'
    testCompile 'junit:junit:4.12'

    //追加
    compile 'frankiesardo:icepick:3.2.0'
    provided 'frankiesardo:icepick-processor:3.2.0'
}

```

```java:MapsActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import icepick.Icepick;
import icepick.State;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


    @State
    LatLng cLocation = new LatLng(35.658622, 139.745530);

    @State
    float cZoom = 20;

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Icepick.restoreInstanceState(this, savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(cLocation,cZoom));
        mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
                cZoom = mMap.getCameraPosition().zoom;
                cLocation = mMap.getCameraPosition().target;
                Toast.makeText(getApplicationContext(), "ズーム" + cZoom + ":" + "経緯緯度" + cLocation.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }
}

```
## まとめ

数行、数分で実装できるのでicepickは便利。