Android公開当初からあまりアップデートのなかったGoogleMapsでしたが、
とうとうアップデートされ、v2という形になりました。
非常に使いやすくなったのでぜひ使ってみてください。
以下では地図の表示までを行います。
地図の表示までの作業は主に、「APIキーの生成」、「Androidプロジェクトの作成」の2つです。
●APIキー生成手順
このAPIキーがあればGoogleMapsが表示されます。
・キーストアのSHA1をメモる
--------------------
キーストアはどこにあるのか?
デバッグの場合
Eclipseの設定から[Android->Build->Default debug keystore]で指定されているものがデバッグ用のキーストア。
リリースの場合
自分で作成して保管しているはず
--------------------
SHA1を取得するコマンドを実行する
keytool -list -v -keystore [キーストア]
※デバッグキーストアの場合はパスワード不要
・Google API Consoleからプロジェクト作成
https://code.google.com/apis/console/
・左側の[Services]から[Google Maps Android API v2]をONにする
・API Accessから[Create new Android key]を押す
・空欄に以下を入力
[メモったキーストアのSHA1;アプリのパッケージ名]
例:45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;com.example
・作成するとAPIKeyが表示されているのでメモる。
APIキー準備完了
●デモ(東京タワーとスカイツリーを別の地図上に表示する)
・SDK ManagerでGoogle Play servicesをインストール
・Google Play servicesのライブラリをEclipseにインポート
{android-sdk}/extras/google/google_play_services/libproject/google-play-services_lib
※GoogleMaps等を使うために必要なライブラリ(isLibrary形式)
・プロジェクトのライブラリとして加える
・Androidプロジェクトを作成
※プロジェクトのパッケージ名はGoogleAPIで指定したものと合わせること
・以下のファイルを作る
public class MainActivity extends FragmentActivity { @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#EDEDED" android:orientation="vertical" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="4dp" android:layout_weight="1" class="com.google.android.gms.maps.SupportMapFragment" map:cameraTargetLat="35.658599" map:cameraTargetLng="139.745443" map:cameraTilt="60" map:cameraZoom="18" /> <fragment android:id="@+id/map2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="4dp" android:layout_weight="1" class="com.google.android.gms.maps.SupportMapFragment" map:cameraTargetLat="35.7105827" map:cameraTargetLng="139.811487" map:cameraTilt="60" map:cameraZoom="18" /> </LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 必要なパーミッション群 --> <!-- 通信に必須 --> <uses-permission android:name="android.permission.INTERNET" /> <!-- 地図キャッシュ --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- GPS取得に必須 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- GoogleMaps表示 --> <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <!-- OpenGLES2.0 --> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <!-- GoogleMapsを使用するAPIキーとか --> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="ここにAPIキーを入れる" /> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>(※com.example.mapdemoを自分のパッケージ名で置き換えること)
(※applicationのmata-dataを自分のAPIキーで置き換えること)
デモ画像
地図複数同時表示や角度・場所の指定もレイアウトだけでできました。
コレは便利ですね。
質問してもよろしいでしょうか?
返信削除このページの手順どおり進めておりますが、activity_main.xmlファイルを作成するときmap:camera~でエラーがでます。
内容は「Unexpected namespace prefix "map" found for tag fragment」です。
どうしたら解決できますでしょうか?
アンドロイド初心者です。どうかお力を貸してください。
よろしくお願いします。
ブログ見ていただきありがとうございます。
削除質問の件ですが、以下を試してください。
1.XMLに以下の記述があるか確認する。
xmlns:map="http://schemas.android.com/apk/res-auto"
2.プロジェクト→クリーンを実行する
もし直らないようであれば、以下のサイトで解決されているみたいなので参照してください。
http://potato777.hatenablog.com/entry/2013/03/22/004745
http://stackoverflow.com/questions/15024430/android-google-maps-fragment-in-the-xml-i-get-unexpected-namespace-prefix
回答ありがとうございます。
削除プロジェクト→クリーンでエラーが消えました!
ただ、エミュレータを立ち上げてアプリを動かそうとするとエラーが出て起動できませんでした。
ログを見てみるとレイアウトxmlのfragmentの行でエラーが出ているようでした。
また作成したアプリを実機にインストールして起動すると問題なく動作しました。
恐らくエミュレータの設定の問題かなと思うのですが、よく分かりません。
難しいですね…。もう少し調べてみようと思います。
ありがとうございました!