「Google Maps Android API V2を使ってみる」では、Google Mapsの表示をLayoutのactive_main.xmlに記述 していましたが、今回地図の表示はMainActivity.javaにフルコードで記述してみました。
また、本番用にLegal Notices(免責事項)の表示用のコードも追加。
初期位置以外に、現在地表示の機能もありますので、初期位置に復帰するコードもつけておきました。
MainActivityのコードは、位置を指定して表示する場合と、現在位置を取得して表示する場合の2つを分けています。
使い方
Google Maps Android API V2を使ってみる では、5つほどのステップをふんでいました。
1:Maps API SDKのインストール(Google Play servicesのライブラリをEclipseにインポート)
2:Google Play servicesライブラリのプロジェクトを作成
3:Google Maps V2を表示するアプリのプロジェクトを作成
4:MapsのAPIキー取得
5:取得したキーをプロジェクトに登録して、アプリをスマホにインストール
今回は単純に3:のみ記述しておきます。
新しいプロジェクトを作成
メニュのFile -> New -> Projectで新しいプロジェクトを作成。
「Android Application Project」を使います。
画像の情報は例です。適当に変えてください。
Targetは2.3.3以上です。
Themeはなしにしておきます。
以後、Nextをクリックして、最後にFinish。
Package Explorerに表示されたプロジェクト名を右クリックして、「Properties」を選択。
「is Library」にチェックが入っていないのを確認して「Add」をクリック。
「google-play-services_lib」を選んで、OK。
Google Maps v2を表示するため、今回は3つのファイルをいじります。
○MainActivity.java
○AndroidManifest.xml
○activity_main.xml
メニュの部分で、Legal Noticesの表示と、初期位置に復帰するコードを記述しておきました。
MainActivity.java(位置を指定して表示)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
<span class="keyword">import</span> android.location.Criteria; <span class="keyword">import</span> android.location.Location; <span class="keyword">import</span> android.location.LocationManager; <span class="keyword">import</span> android.os.Bundle; <span class="keyword">import</span> android.view.Menu; <span class="keyword">import</span> android.view.MenuItem; <span class="keyword">import</span> android.app.AlertDialog; <span class="keyword">import</span> android.app.Dialog; <span class="keyword">import</span> android.util.Log; <span class="keyword">import</span> android.widget.Toast; <span class="keyword">import</span> android.support.v4.app.Fragment; <span class="keyword">import</span> android.support.v4.app.FragmentActivity; <span class="keyword">import</span> android.support.v4.app.FragmentManager; <span class="keyword">import</span> android.support.v4.app.FragmentTransaction; <span class="keyword">import</span> com.google.android.gms.maps.SupportMapFragment; <span class="keyword">import</span> com.google.android.gms.common.GooglePlayServicesUtil; <span class="keyword">import</span> com.google.android.gms.common.ConnectionResult; <span class="keyword">import</span> com.google.android.gms.maps.CameraUpdate; <span class="keyword">import</span> com.google.android.gms.maps.CameraUpdateFactory; <span class="keyword">import</span> com.google.android.gms.maps.model.CameraPosition; <span class="keyword">import</span> com.google.android.gms.maps.GoogleMap; <span class="keyword">import</span> com.google.android.gms.maps.GoogleMapOptions; <span class="keyword">import</span> com.google.android.gms.maps.UiSettings; <span class="keyword">import</span> com.google.android.gms.maps.model.LatLng; <span class="keyword">import</span> android.widget.TextView; <span class="keyword">public</span> <span class="keyword">class</span> MainActivity <span class="keyword">extends</span> FragmentActivity { <span class="keyword">private</span> SupportMapFragment mapFragment; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String HOGE_MAP_FRAGMENT_TAG = <span class="str">"maps_fragment"</span>; <span class="keyword">private</span> GoogleMap gMap; <span class="keyword">public</span> <span class="keyword">double</span> latitude = <span class="num">0</span>; <span class="keyword">public</span> <span class="keyword">double</span> longitude = <span class="num">0</span>; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MENU_A = <span class="num">0</span>; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MENU_B = <span class="num">1</span>; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> RQS_GooglePlayServices = <span class="num">1</span>; <span class="keyword">private</span> TextView textview; @Override <span class="keyword">protected</span> <span class="keyword">void</span> onCreate(Bundle savedInstanceState) { <span class="keyword">super</span>.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview = (TextView) findViewById(R.id.tv_location); <span class="rem">//Google Play Servicesが使えるかどうかのステータス</span> <span class="keyword">int</span> status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); <span class="rem">// Showing status</span> <span class="keyword">if</span>(status!=ConnectionResult.SUCCESS){ <span class="rem">// Google Play Services が使えない場合</span> <span class="keyword">int</span> requestCode = <span class="num">10</span>; Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, <span class="keyword">this</span>, requestCode); dialog.show(); }<span class="keyword">else</span> { <span class="rem">// Google Play Services が使える場合</span> <span class="rem">//activity_main.xmlのSupportMapFragmentへの参照を取得</span> SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); <span class="rem">//fragmentからGoogleMap objectを取得</span> gMap = fm.getMap(); <span class="rem">//Google MapのMyLocationレイヤーを使用可能にする</span> gMap.setMyLocationEnabled(true); gMap.setIndoorEnabled(true); gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); gMap.setTrafficEnabled(true); <span class="rem">//システムサービスのLOCATION_SERVICEからLocationManager objectを取得</span> LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); <span class="rem">//retrieve providerへcriteria objectを生成</span> Criteria criteria = <span class="keyword">new</span> Criteria(); <span class="rem">//Best providerの名前を取得</span> String provider = locationManager.getBestProvider(criteria, true); } } @Override <span class="keyword">public</span> <span class="keyword">boolean</span> onCreateOptionsMenu(Menu menu) { <span class="rem">// Inflate the menu; this adds items to the action bar if it is present.</span> <span class="rem">//getMenuInflater().inflate(R.menu.activity_main, menu);</span> menu.add(<span class="num">0</span>, MENU_A, <span class="num">0</span>, <span class="str">"Home"</span>); menu.add(<span class="num">0</span>, MENU_B, <span class="num">0</span>, <span class="str">"Legal Notices"</span>); <span class="keyword">return</span> true; } <span class="keyword">public</span> <span class="keyword">boolean</span> onOptionsItemSelected(MenuItem item) { <span class="keyword">switch</span> ( item.getItemId() ){ <span class="keyword">case</span> MENU_A: <span class="rem">//復帰</span> LatLng HOME = <span class="keyword">new</span> LatLng(<span class="num">35.02878398517723</span>,<span class="num">135.77929973602295</span>); moveCamera2Target(true,HOME,<span class="num">18.0</span>f,<span class="num">60.0</span>f,<span class="num">0.0</span>f); <span class="keyword">return</span> true; <span class="keyword">case</span> MENU_B: <span class="rem">//Legal Notices</span> String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext()); AlertDialog.Builder LicenseDialog = <span class="keyword">new</span> AlertDialog.Builder(MainActivity.<span class="keyword">this</span>); LicenseDialog.setTitle(<span class="str">"Legal Notices"</span>); LicenseDialog.setMessage(LicenseInfo); LicenseDialog.show(); <span class="keyword">return</span> true; } <span class="keyword">return</span> false; } @Override <span class="keyword">protected</span> <span class="keyword">void</span> onResume(){ <span class="keyword">super</span>.onResume(); <span class="keyword">int</span> resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); <span class="keyword">if</span> (resultCode == ConnectionResult.SUCCESS){ Toast.makeText(getApplicationContext(), <span class="str">"isGooglePlayServicesAvailable SUCCESS"</span>, Toast.LENGTH_LONG).show(); <span class="rem">//初期位置</span> LatLng target = <span class="keyword">new</span> LatLng(<span class="num">35.02878398517723</span>,<span class="num">135.77929973602295</span>); textview.setText(<span class="str">"35.02878398517723,135.77929973602295"</span>); <span class="rem">//ズームレベル</span> <span class="keyword">float</span> zoom = <span class="num">18.0</span>f; <span class="rem">//チルトアングル</span> <span class="keyword">float</span> tilt = <span class="num">90.0</span>f; <span class="rem">// 0.0 - 90.0</span> <span class="rem">//向き</span> <span class="keyword">float</span> bearing = <span class="num">0.0</span>f; <span class="rem">//CameraUpdate</span> CameraPosition pos = <span class="keyword">new</span> CameraPosition(target, zoom, tilt, bearing); CameraUpdate camera = CameraUpdateFactory.newCameraPosition(pos); gMap.moveCamera(camera); <span class="rem">//UiSettings</span> UiSettings uiSet = gMap.getUiSettings(); uiSet.setZoomControlsEnabled(true); uiSet.setZoomGesturesEnabled(true); <span class="rem">//uiSet.setCompassEnabled(true);</span> uiSet.setRotateGesturesEnabled(true); }<span class="keyword">else</span>{ GooglePlayServicesUtil.getErrorDialog(resultCode, <span class="keyword">this</span>, RQS_GooglePlayServices); } } <span class="keyword">private</span> <span class="keyword">void</span> moveCamera2Target(<span class="keyword">boolean</span> animation_effect,LatLng target,<span class="keyword">float</span> zoom,<span class="keyword">float</span> tilt,<span class="keyword">float</span> bearing) { CameraPosition pos = <span class="keyword">new</span> CameraPosition(target, zoom, tilt, bearing); CameraUpdate camera = CameraUpdateFactory.newCameraPosition(pos); <span class="keyword">if</span> (animation_effect == true) { <span class="rem">//</span> gMap.animateCamera(camera); } <span class="keyword">else</span> { <span class="rem">//</span> gMap.moveCamera(camera); } } } |
MainActivity.java(現在位置を取得して表示)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
<span class="keyword">import</span> android.location.Criteria; <span class="keyword">import</span> android.location.Location; <span class="keyword">import</span> android.location.LocationManager; <span class="keyword">import</span> android.location.LocationListener; <span class="keyword">import</span> android.os.Bundle; <span class="keyword">import</span> android.view.Menu; <span class="keyword">import</span> android.view.MenuItem; <span class="keyword">import</span> android.app.AlertDialog; <span class="keyword">import</span> android.app.Dialog; <span class="keyword">import</span> android.util.Log; <span class="keyword">import</span> android.widget.Toast; <span class="keyword">import</span> android.support.v4.app.Fragment; <span class="keyword">import</span> android.support.v4.app.FragmentActivity; <span class="keyword">import</span> android.support.v4.app.FragmentManager; <span class="keyword">import</span> android.support.v4.app.FragmentTransaction; <span class="keyword">import</span> com.google.android.gms.maps.SupportMapFragment; <span class="keyword">import</span> com.google.android.gms.common.GooglePlayServicesUtil; <span class="keyword">import</span> com.google.android.gms.common.ConnectionResult; <span class="keyword">import</span> com.google.android.gms.maps.CameraUpdate; <span class="keyword">import</span> com.google.android.gms.maps.CameraUpdateFactory; <span class="keyword">import</span> com.google.android.gms.maps.model.CameraPosition; <span class="keyword">import</span> com.google.android.gms.maps.GoogleMap; <span class="keyword">import</span> com.google.android.gms.maps.GoogleMapOptions; <span class="keyword">import</span> com.google.android.gms.maps.UiSettings; <span class="keyword">import</span> com.google.android.gms.maps.model.LatLng; <span class="keyword">import</span> android.widget.TextView; <span class="keyword">public</span> <span class="keyword">class</span> MainActivity <span class="keyword">extends</span> FragmentActivity <span class="keyword">implements</span> LocationListener { <span class="keyword">private</span> SupportMapFragment mapFragment; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String HOGE_MAP_FRAGMENT_TAG = <span class="str">"maps_fragment"</span>; <span class="keyword">private</span> GoogleMap gMap; <span class="keyword">public</span> <span class="keyword">double</span> latitude = <span class="num">0</span>; <span class="keyword">public</span> <span class="keyword">double</span> longitude = <span class="num">0</span>; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MENU_A = <span class="num">0</span>; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MENU_B = <span class="num">1</span>; <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> RQS_GooglePlayServices = <span class="num">1</span>; <span class="keyword">private</span> TextView textview; @Override <span class="keyword">protected</span> <span class="keyword">void</span> onCreate(Bundle savedInstanceState) { <span class="keyword">super</span>.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview = (TextView) findViewById(R.id.tv_location); <span class="rem">//Google Play Servicesが使えるかどうかのステータス</span> <span class="keyword">int</span> status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); <span class="rem">// Showing status</span> <span class="keyword">if</span>(status!=ConnectionResult.SUCCESS){ <span class="rem">// Google Play Services が使えない場合</span> <span class="keyword">int</span> requestCode = <span class="num">10</span>; Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, <span class="keyword">this</span>, requestCode); dialog.show(); }<span class="keyword">else</span> { <span class="rem">// Google Play Services が使える場合</span> <span class="rem">//activity_main.xmlのSupportMapFragmentへの参照を取得</span> SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); <span class="rem">//fragmentからGoogleMap objectを取得</span> gMap = fm.getMap(); <span class="rem">//Google MapのMyLocationレイヤーを使用可能にする</span> gMap.setMyLocationEnabled(true); gMap.setIndoorEnabled(true); gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); gMap.setTrafficEnabled(true); <span class="rem">//システムサービスのLOCATION_SERVICEからLocationManager objectを取得</span> LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); <span class="rem">//retrieve providerへcriteria objectを生成</span> Criteria criteria = <span class="keyword">new</span> Criteria(); <span class="rem">//Best providerの名前を取得</span> String provider = locationManager.getBestProvider(criteria, true); <span class="rem">//現在位置を取得</span> Location location = locationManager.getLastKnownLocation(provider); <span class="keyword">if</span>(location!=null){ onLocationChanged(location); } locationManager.requestLocationUpdates(provider, <span class="num">20000</span>, <span class="num">0</span>, <span class="keyword">this</span>); } } @Override <span class="keyword">public</span> <span class="keyword">boolean</span> onCreateOptionsMenu(Menu menu) { <span class="rem">// Inflate the menu; this adds items to the action bar if it is present.</span> <span class="rem">//getMenuInflater().inflate(R.menu.activity_main, menu);</span> menu.add(<span class="num">0</span>, MENU_A, <span class="num">0</span>, <span class="str">"Home"</span>); menu.add(<span class="num">0</span>, MENU_B, <span class="num">0</span>, <span class="str">"Legal Notices"</span>); <span class="keyword">return</span> true; } <span class="keyword">public</span> <span class="keyword">boolean</span> onOptionsItemSelected(MenuItem item) { <span class="keyword">switch</span> ( item.getItemId() ){ <span class="keyword">case</span> MENU_A: <span class="rem">//復帰</span> LatLng HOME = <span class="keyword">new</span> LatLng(<span class="num">35.02878398517723</span>,<span class="num">135.77929973602295</span>); moveCamera2Target(true,HOME,<span class="num">18.0</span>f,<span class="num">60.0</span>f,<span class="num">0.0</span>f); <span class="keyword">return</span> true; <span class="keyword">case</span> MENU_B: <span class="rem">//Legal Notices</span> String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext()); AlertDialog.Builder LicenseDialog = <span class="keyword">new</span> AlertDialog.Builder(MainActivity.<span class="keyword">this</span>); LicenseDialog.setTitle(<span class="str">"Legal Notices"</span>); LicenseDialog.setMessage(LicenseInfo); LicenseDialog.show(); <span class="keyword">return</span> true; } <span class="keyword">return</span> false; } @Override <span class="keyword">protected</span> <span class="keyword">void</span> onResume(){ <span class="keyword">super</span>.onResume(); } <span class="keyword">private</span> <span class="keyword">void</span> moveCamera2Target(<span class="keyword">boolean</span> animation_effect,LatLng target,<span class="keyword">float</span> zoom,<span class="keyword">float</span> tilt,<span class="keyword">float</span> bearing) { CameraPosition pos = <span class="keyword">new</span> CameraPosition(target, zoom, tilt, bearing); CameraUpdate camera = CameraUpdateFactory.newCameraPosition(pos); <span class="keyword">if</span> (animation_effect == true) { <span class="rem">//</span> gMap.animateCamera(camera); } <span class="keyword">else</span> { <span class="rem">//</span> gMap.moveCamera(camera); } } @Override <span class="keyword">public</span> <span class="keyword">void</span> onLocationChanged(Location location) { <span class="rem">//現在位置の緯度を取得</span> latitude = location.getLatitude(); <span class="rem">//現在位置の経度を取得</span> longitude = location.getLongitude(); textview.setText(latitude + <span class="str">","</span> + longitude); <span class="rem">//reverse_geocode(latitude,longitude);</span> <span class="rem">//現在位置からLatLng objectを生成</span> LatLng latLng = <span class="keyword">new</span> LatLng(latitude, longitude); <span class="rem">//Google Mapに現在地を表示</span> gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); <span class="rem">//Google Mapの Zoom値を指定</span> gMap.animateCamera(CameraUpdateFactory.zoomTo(<span class="num">15</span>)); } @Override <span class="keyword">public</span> <span class="keyword">void</span> onProviderDisabled(String provider) { <span class="rem">// TODO Auto-generated method stub</span> } @Override <span class="keyword">public</span> <span class="keyword">void</span> onProviderEnabled(String provider) { <span class="rem">// TODO Auto-generated method stub</span> } @Override <span class="keyword">public</span> <span class="keyword">void</span> onStatusChanged(String provider, <span class="keyword">int</span> status, Bundle extras) { <span class="rem">// TODO Auto-generated method stub</span> } } |
AndroidManifest.xml
下のコードで、「APIキー」と書かれている部分は、APIキー取得後に修正してください。
xxxxxの部分は、ご自分のパッケージ名で置き換えてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<span class="tag"><?xml version="1.0" encoding="utf-8"?></span> <span class="tag"><manifest <span class="attr">xmlns:android=</span><span class="value">"http://schemas.android.com/apk/res/android"</span> <span class="attr">package=</span><span class="value">"<span style="color: #ff0000;">xxxxx</span>"</span> <span class="attr">android:versionCode=</span><span class="value">"1"</span> <span class="attr">android:versionName=</span><span class="value">"1.0"</span> ></span> <span class="tag"><uses-sdk <span class="attr">android:minSdkVersion=</span><span class="value">"9"</span> <span class="attr">android:targetSdkVersion=</span><span class="value">"17"</span> /></span> <span class="tag"><uses-permission <span class="attr">android:name=</span><span class="value">"<span style="color: #ff0000;">xxxxx</span>.permission.MAPS_RECEIVE"</span>/></span> <span class="tag"><uses-permission <span class="attr">android:name=</span><span class="value">"android.permission.INTERNET"</span>/></span> <span class="tag"><uses-permission <span class="attr">android:name=</span><span class="value">"android.permission.WRITE_EXTERNAL_STORAGE"</span>/></span> <span class="tag"><uses-permission <span class="attr">android:name=</span><span class="value">"com.google.android.providers.gsf.permission.READ_GSERVICES"</span>/></span> <span class="tag"><uses-permission <span class="attr">android:name=</span><span class="value">"android.permission.ACCESS_COARSE_LOCATION"</span>/></span> <span class="tag"><uses-permission <span class="attr">android:name=</span><span class="value">"android.permission.ACCESS_FINE_LOCATION"</span>/></span> <span class="tag"><uses-feature <span class="attr">android:glEsVersion=</span><span class="value">"0x00020000"</span> <span class="attr">android:required=</span><span class="value">"true"</span>/></span> <span class="tag"><application <span class="attr">android:allowBackup=</span><span class="value">"true"</span> <span class="attr">android:icon=</span><span class="value">"@drawable/ic_launcher"</span> <span class="attr">android:label=</span><span class="value">"@string/app_name"</span> <span class="attr">android:theme=</span><span class="value">"@style/AppTheme"</span> ></span> <span class="tag"><meta-data <span class="attr">android:name=</span><span class="value">"com.google.android.maps.v2.API_KEY"</span> <span class="attr">android:value=</span><span class="value">"<span style="color: #ff0000;">APIキー</span>"</span> /></span> <span class="tag"><activity <span class="attr">android:name=</span><span class="value">"<span style="color: #ff0000;">xxxxx</span>.MainActivity"</span> <span class="attr">android:label=</span><span class="value">"@string/app_name"</span> ></span> <span class="tag"><intent-filter></span> <span class="tag"><action <span class="attr">android:name=</span><span class="value">"android.intent.action.MAIN"</span> /></span> <span class="tag"><category <span class="attr">android:name=</span><span class="value">"android.intent.category.LAUNCHER"</span> /></span> <span class="tag"></intent-filter></span> <span class="tag"></activity></span> <span class="tag"></application></span> <span class="tag"></manifest></span> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span class="tag"><RelativeLayout <span class="attr">xmlns:android=</span><span class="value">"http://schemas.android.com/apk/res/android"</span> <span class="attr">xmlns:tools=</span><span class="value">"http://schemas.android.com/tools"</span> <span class="attr">android:layout_width=</span><span class="value">"match_parent"</span> <span class="attr">android:layout_height=</span><span class="value">"match_parent"</span> <span class="attr">tools:context=</span><span class="value">".MainActivity"</span> ></span> <span class="tag"><TextView <span class="attr">android:id=</span><span class="value">"@+id/textview1"</span> <span class="attr">android:layout_width=</span><span class="value">"wrap_content"</span> <span class="attr">android:layout_height=</span><span class="value">"wrap_content"</span> /></span> <span class="tag"><fragment <span class="attr">xmlns:android=</span><span class="value">"http://schemas.android.com/apk/res/android"</span> <span class="attr">android:id=</span><span class="value">"@+id/map"</span> <span class="attr">android:layout_width=</span><span class="value">"match_parent"</span> <span class="attr">android:layout_height=</span><span class="value">"match_parent"</span> <span class="attr">android:layout_below=</span><span class="value">"@id/textview1"</span> <span class="attr">class=</span><span class="value">"com.google.android.gms.maps.SupportMapFragment"</span> /></span> <span class="tag"></RelativeLayout> </span> |
起動時の画面
2D<->3D切り替えやフロアプラン、ズームなどについては、
Google Maps Android API V2を使ってみる を参照。
前回から追加された機能
ここをタップするとGPSで現在地を取得し、地図を表示します。
メニュです。
左の「Home」をタップすると、初期位置に復帰します。
右の「Legal Notices」をタップすると、免責事項についての説明を表示します。
その他
UiSettingsで、setRotateGesturesEnabled(true)にしておけば、2本の指で回転させるよう(ちょうどペットボトルのキャップをいじるような動き)にすると、 地図が回転します。指が2本であれば、どの指を使ってもいいのですが…。
Leave a Reply