その他
    ホーム 技術発信 DoRuby AndroidでGoogle Mapsを表示

    AndroidでGoogle Mapsを表示

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    AndroidでGoogle Mapsを表示するための方法をご紹介します。

     Maps APIキーの取得

    Android上でGoogle Mapsを使用するためにMaps APIキーが必要です。

    その際、Googleのアカウントと証明書が必要となります。

    証明書はデバッグ用のものが以下の手順で取得できます。

    $ keytool -list -keystore ~/.android/debug.keystore
    ...
    Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98
    

    パスワードを聞かれますが、入力せずにEnterを押します。

    次に以下のサイトにアクセスします。

    Android Maps API Key

    規約確認のチェックボックスをONにし、

    先ほど出力したフィンガープリントを入力して、「Generate API Key」ボタンを押すと、

    Maps Keyを取得できます。

     Google APIsインストール

    Android Mapsを使用するためにはGoogle APIを使用します。

    プロジェクト作成時のビルドターゲットに「Google APIs」が無い場合は、Android SDK and AVD Managerでインストールしてください。

    ※Google APIsはプラットフォーム別に複数あるので、開発に使うプラットフォームのものは必ずインストールします。 まあ全部インストールしちゃってもいいと思います。

    1. Android SDK and AVD Managerを開く

    (開発にEclipseを使用している場合は、[ウィンドウ] – [Android SDK and AVD Manager] などで開けます)

    2. Available PackageでインストールするGoogle APIsにチェックを入れる

    3. [Install Selected]をクリックしてインストール

     AVDの作成

    Google APIs用のAVD(Android Virtual Device)を作成しておきます。

    Google APIsをインストールした時と同じようにAndroid SDK and AVD Managerで作成します。

    1. Android SDK and AVD Managerを開く

    (開発にEclipseを使用している場合は、[ウィンドウ] – [Android SDK and AVD Manager] などで開けます)

    2. Virtual Devicesで[新規]をクリック

    3. 「名前」は適当なものを入れ、「ターゲット」には開発で使用するGoogle APIsのプラットフォームを入力して、[Create AVD]をクリック

     サンプルプロジェクトの作成

    最後に簡単にGoogle Mapを表示するためのサンプルプロジェクトを紹介します。

    新規プロジェクトの作成では、ビルドターゲットに先ほどインストールしたGoogle APIsを選択します。

    package com.exapmple.androidmaps2;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    import android.os.Bundle;
    import com.google.android.maps.*;
     
    public class AndroidMaps2 extends MapActivity {
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            MapView map = new MapView(this, "[Android Maps API Key]");
            map.setEnabled(true);
            map.setClickable(true);
            setContentView(map);
        }
     
        @Override
         protected boolean isRouteDisplayed() {
            return false;
        }
    }
    

    [Android Maps API Key]に準備で取得したAndroid Maps API Keyを入力して下さい。

    AndroidManifest.xmlは下記のようになります。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidmaps" android:versioncode="1" android:versionname="1.0">
        <uses-sdk android:minsdkversion="1.5" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".AndroidMaps" android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    		 <uses-library android:name="com.google.android.maps" /> ←追加
        </application>
        <uses-permission android:name="android.permission.INTERNET" /> ←追加
    </manifest>
    

    これでGoogle Mapが表示されます!

    記事を共有