LoginSignup
3
3

More than 5 years have passed since last update.

Xamarin.Android で Google Maps Android API v2 を使う

Posted at

だんだん自力でサンプルプログラム書くのに疲れてきたので、MonoDroid のサンプル集に頼ることにします。

すごく豊富なので、これひと通り動かせば大抵のことはできるようになるんじゃないか。。。

というわけで、この中の MapsAndLocationDemo_v2 を動かしてみます。Readme.md に沿って。

ソースを取得する

まず monodroid-samples を Clone しときます。

$git clone git://github.com/xamarin/monodroid-samples.git

クライアントライブラリをビルドする(Building the Client Library)

ここでいうクライアントライブラリとは、Android SDK に含まれる Google Play services の事です。
この準備は本家Android開発で Google Maps Android API v2 を使うときに必要なので、これが何者かは、

などを参考に。
SDK で Google Play services が取得できていれば、

{※あなたのAndroidSDKディレクトリ}/extras/google/google_play_services/libproject/google-play-services_lib ができているはず。

※Xamarin.Android で使われる Android SDK は、Xamarin Studio のPreferences → SDK Locations → Android で確認できます。

"sdklocations"

google-play-services_lib へ移動して、Ant 用のビルドファイルを生成します。

$cd google-play-services_lib
$Android update project --path .
$ant debug

Readme.md では android project update -p . って書いてあるけど上のコマンドじゃないとうまくいかないんですけど?
あと {あなたのAndroidSDKディレクトリ}/tools にパス通しておかないと android コマンド使えないです。ant も(ry

ant の実行に成功すると /bin の中に classes.jar とかができてるはずです。

次に、Xamarin Studio で MapsAndLocationDemo.sln を開いて、GooglePlayService プロジェクトを展開します。
すると、project.properties` ファイルが赤くなってる(ちゃんと指定した位置に google-play-services_lib をコピってれば赤くなってないかも)ので、これを一度削除して、追加しなおします。リンクファイルとして、ね。

一連の画像貼っときます。

"red"
"red"
"red"

Google Maps v2 API Key

Google Maps v2 API Key の取得も本家と同じですが、
ディレクトリが、/Users/[USERNAME]/.local/share/Xamarin/Mono for Android/debug.keystore なのでそこだけ注意。(←Mac の場合。Win は Readme.md 見て)

該当のディレクトリへ移動して、以下のコマンドで表示されるキーの内、SHA-1 をコピーします。

$ keytool -V -list -keystore debug.keystore -alias androiddebugkey -storepass android -keypass android

あとは、

を参考に、Google API Console から、API key を取得します。
API Key を取得する際に必要なパッケージ名、取得した API Key を貼り付ける場所は、いずれも MapsAndLocationDemo プロジェクトの AndroidManifest.xml にあるので、先に開いておくとよいでしょう。

"apikey"

API Key を取得したら、com.google.android.maps.v2.API_KEY のところに貼り付けて準備完了です。

A Note About Google Play Services

Google Play Services が端末にインストールされてないと動かんよ、つまりエミュレータでは動かんよ。Google Play Services のインストールの仕方など、私の知ったことではない。だそうです。

動かしてみる

「実機で」動かしてみます。

"play"

動いてます。(上は Show Map with Overlays を動かしたところ)

今回はここまで。コードの深追いは日を改めて。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3