Googleマップを使用するAndroidアプリを作成する

■Googleマップを使用したアプリの作成方法(サイト)
http://code.google.com/intl/ja/android/add-ons/google-apis/maps-overview.html

■AndroidSDKリファレンス
・android-sdk-windows-1.5_r2.zip
解凍後のフォルダ内
・docs/reference/packages.html

Androidアプリを作成する

「ファイル」→「新規」→「Androidプロジェクト」
プロジェクト名、アプリケーション名、パッケージ名を入力、
ターゲット名を選択する。Googleマップを使用するため、ターゲット名は「Google APIs」を選択。
今回は「Create Activity」のチェックをはずします。

完了ボタンをクリック。

Googleマップを表示するクラスを作成する

HelloMap/src/android/sample
にフォーカスをあて、右クリックし、
「新規」→「クラス」を選択します。
名前にクラス名を入力、スーパークラスにcom.google.android.maps.MapActivityを入力(もしくは参照から入力)します。

完了ボタンをクリック。

HelloMapActivity.java

package android.sample;

import android.os.Bundle;

import com.google.android.maps.MapActivity;

public class HelloMapActivity extends MapActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

上記のように記述をします。
Googleマップを使用するにはMapActivityを継承するクラスを作成する必要があります。
MapActivityを継承するとisRouteDisplayed()メソッドが必須となります。

main.xml

map画面のレイアウトを設定します。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="@string/api_key"
        />
</LinearLayout>

        android:apiKey="@string/api_key"

でAPIキーを指定しています。
GoogleマップにアクセスするためにAPIキー指定が必要なためです。

strings.xml

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">HelloMap</string>
    <string name="api_key">【APIキー】</string>
</resources>

strings.xmlにAPIキーを記述します。

APIキーは、登録用ページで画面の指示に従って取得してください。
http://code.google.com/android/maps-api-signup.html

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="android.sample"
      android:versionCode="1"
      android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="com.google.android.maps"/>

        <activity android:name=".HelloMapActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

        <activity android:name=".HelloMapActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

activity(HelloMapActivity)を追加します。

mapを使用するため

        <uses-library android:name="com.google.android.maps"/>

を追加し、
mapでインターネットを使用するため

    <uses-permission android:name="android.permission.INTERNET"/>

を追加します。

Android Virtual Devices Manager

Android Virtual Devices Managerの設定が必要です。
アイコンをクリックし、AVDを作成します。

Create AVD
名前:[任意]
ターゲット:Google APIs - 1.5
Skin :Default
を入力、選択し
Create AVDボタンをクリック。

ダイアログが表示されるので
OKボタンをクリック。

リストに追加されているのを確認したら、
完了ボタンをクリック。

Android Virtual Devices Manager

アプリを実行します。

Android開発補助

GDDフォン等、Googleが配布したSIMロックフリー、技適取得のAndroid端末で3Gを使ったアプリ動作確認やデモで限られた時間で通信する場合は、以下のサービスが便利です。

Android(アンドロイド) 開発者向けチャージ済み SIM パッケージ

安心して開発に取り組めるので大変役に立ちます。

このページ「google Map を使用するアプリ」の上へ