AndroidでGoogleMapのカメラをプログラムからズームします。
※API12より前で、SupportMapFragmentを使用するバージョン。
【準備】
1.「Google Play services SDKをワークスペースにインポートする方法」で「Google Play services SDK」をワークスペースにインポートしておきます。
2.「Google APIsのGoogle Maps Android API v2のAPIキーを取得する方法」の手順で、「Google Maps Android API v2」のAPIキーを取得しておきます。
※APIキーの取得に指定するパッケージ名は「com.example.androidsample34_013」です。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample34-013-GoogleMapV2CameraZoom」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidsample34_013 |
| プロジェクト名(Project Name) | AndroidSample34-013-GoogleMapV2CameraZoom |
| パッケージ名(Package Name) | com.example.androidsample34_013 |
| Build SDK | API 8 |
| Minimum Required SDK | API 8 |
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsample34_013"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<permission
android:name="com.example.androidsample34_013.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.androidsample34_013.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_OWN_KEY" />
<activity
android:name="com.example.androidsample34_013.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>
2.「YOUR_OWN_KEY」を【準備】で取得しておいた、APIキーに変更。3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
4.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「res/layout/activity_main.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickMoveZoomOut"
android:text="Move(-)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickMoveZoomIn"
android:text="Move(+)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickAnimateZoomOut"
android:text="Animate(-)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickAnimateZoomIn"
android:text="Animate(+)" />
</LinearLayout>
</LinearLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「src/com/example/androidsample34_013/MainActivity.java」は以下の通り。
package com.example.androidsample34_013;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.example.androidsample34_013.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMap();
}
private void initMap() {
this.googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
this.googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("マーカー"));
}
public void onClickMoveZoomOut(View view) {
googleMap.moveCamera(CameraUpdateFactory.zoomOut());
}
public void onClickMoveZoomIn(View view) {
googleMap.moveCamera(CameraUpdateFactory.zoomIn());
}
public void onClickAnimateZoomOut(View view) {
googleMap.animateCamera(CameraUpdateFactory.zoomOut());
}
public void onClickAnimateZoomIn(View view) {
googleMap.animateCamera(CameraUpdateFactory.zoomIn());
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
※エミュレーターでは「Google Play services SDK」が上手く動かないみたいで、実機で実行する必要があるみたいです。
【手順6】
1.以下の様に表示されれば成功です。
以上です。


0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。