Skip to content

Instantly share code, notes, and snippets.

@tsmsogn
Last active December 19, 2015 19:29
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tsmsogn/6006551 to your computer and use it in GitHub Desktop.
[android]Google Play Services SDK
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

2013/07/17

Android で Google Maps API を使う党して見たんだけど、V1 がどうもオワコンっぽかったので V2 を入れることになりそうなのでその手順のとめ

Important: Because it is hard to anticipate the state of each device, you must always check for a compatible Google Play services APK before you access Google Play services features. For many apps, the best time to check is during the onResume() method of the main activity.

メインアクティビティの onResume でチェックするのがいいっぽい

1. A recent version of the Google Play Store app is installed, and the most recent Google Play services APK has been downloaded.
2. A recent version of the Google Play Store app is installed, but the most recent Google Play services APK has not been downloaded.
3. An old version of the Google Play Store app, which does not proactively download Google Play services updates, is present.
4. The Google Play services APK is missing or disabled on the device, which might happen if the user explicitly uninstalls or disables it.

上記で言う 1は問題ないけど、それ以外は問題ありなので Google Play Services APK に接続するたびに促さないといけない

1. Query for the status of Google Play services on the device with the isGooglePlayServicesAvailable() method, which returns a result code.
2. If the result code is SUCCESS, then the Google Play services APK is up-to-date, and you can proceed as normal.
3 .If the result code is SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, or SERVICE_DISABLED, then call getErrorDialog() to display an error message to the user, which allows the user to download the APK from the Google Play Store or enable it in the device's system settings.

的な処理をしましょうね

コードで言うとこんな漢字でしょうか?

  int statusCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getApplicationContext());
        if (statusCode == ConnectionResult.SUCCESS) {
            Log.v(TAG, "" + statusCode);
        } else if (statusCode == ConnectionResult.SERVICE_MISSING
                || statusCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED
                || statusCode == ConnectionResult.SERVICE_DISABLED) {
            Log.v(TAG, "" + statusCode);
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode,
                    this, 1);
            dialog.show();
        }

The Google Maps API Key の取得

APIキーは certificate/package のペアにリンクしている certificate だけでもいけるけど、アプリケーション別に certificate を変えるのをおすすめしている

Displaying certificate information

  • Certification と Key はペアである

Debug certificate と Release certificate

Debug certificate

http://developer.android.com/tools/publishing/app-signing.html#debugmode

https://code.google.com/apis/console/#project:94614545578:access

いって

Services いって Google Maps Android API v2On して、API Access いって

$ keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

で得られる SHA1 の値とを ;package.name を足して API key を取得

AndroidManifest.xml へ

 <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="API_KEY" />
  • Required
android.permission.INTERNET Used by the API to download map tiles from Google Maps servers.
android.permission.ACCESS_NETWORK_STATE Allows the API to check the connection status in order to determine whether data can be downloaded.
com.google.android.providers.gsf.permission.READ_GSERVICES Allows the API to access Google web-based services.
android.permission.WRITE_EXTERNAL_STORAGE Allows the API to cache map tile data in the device's external storage area.
  • Recommend
android.permission.ACCESS_COARSE_LOCATION Allows the API to use WiFi or mobile cell data (or both) to determine the device's location.
android.permission.ACCESS_FINE_LOCATION Allows the API to use the Global Positioning System (GPS) to determine the device's location to within a very small area.
<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

https://developers.google.com/maps/documentation/android/map

V2 を使ってみる onCreate()onResume() で呼び出す必要がある


private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            // The Map is verified. It is now safe to manipulate the map.

        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment