「Google Maps Android API V2を使ってみる」、「Google Maps Android API V2を使ってみる2」では、Google Mapsの表示のみをやってみました。
ここでは、地図関連のv2の機能を実装します。
メモみたいなものですが………。
イベント処理
地図をタップ
地図上でタップした時、その位置を取得してToastで表示してみます。
MainActivity.java
1 2 |
import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import android.widget.Toast; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@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){ ・ ・ ・ ・ gMap.setOnMapClickListener(<span class="keyword">new</span> OnMapClickListener() { @Override <span class="keyword">public</span> <span class="keyword">void</span> onMapClick(LatLng point) { String posinfo = <span class="str">"clickpos\n"</span> + <span class="str">"latitude="</span> + point.latitude + <span class="str">", longitude="</span> + point.longitude; Toast.makeText(getApplicationContext(), posinfo, Toast.LENGTH_LONG).show(); } }); } } |
地図を長押し
MainActivity.java
1 2 |
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener; import android.widget.Toast; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@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){ ・ ・ ・ ・ gMap.setOnMapLongClickListener(<span class="keyword">new</span> OnMapLongClickListener() { @Override <span class="keyword">public</span> <span class="keyword">void</span> onMapLongClick(LatLng point) { Toast.makeText(getApplicationContext(), <span class="str">"LongClick"</span>, Toast.LENGTH_LONG).show(); } }); } } |
フリースクロールしてカメラが移動した時の位置を取得してToastで表示
位置はmoveendした時のものです。
MainActivity.java
1 2 |
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener; import android.widget.Toast; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@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){ ・ ・ ・ ・ gMap.setOnCameraChangeListener(<span class="keyword">new</span> OnCameraChangeListener() { @Override <span class="keyword">public</span> <span class="keyword">void</span> onCameraChange(CameraPosition position) { LatLng point = position.target; String posinfo = <span class="str">"latitude="</span> + point.latitude + <span class="str">", longitude="</span> + point.longitude; Toast.makeText(getApplicationContext(), posinfo, Toast.LENGTH_LONG).show(); } }); } } |
情報表示・入力用のActivityを追加
メニュで開きます。
infoのフィールドに入れた情報はMainに戻った時、Toastで表示されます。
メニュのFile -> New で新しいClass(infoActivity.java)を追加。
AndroidManifest.xmlにactivityを追加。
resフォルダーにdrawableフォルダーを追加して、この中にボタン用画像(return.png)を追加。
以下の「com.example」(パッケージ名)は適当に読み替えてください。
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
<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.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="rem">//カメラ移動イベント</span> <span class="keyword">import</span> com.google.android.gms.maps.GoogleMap.OnCameraChangeListener; <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="rem">//</span> <span class="keyword">import</span> android.content.Intent; <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">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> MENU_C = <span class="num">2</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> String posinfo = <span class="str">""</span>; <span class="rem">//</span> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> INFOACTIVITY = <span class="num">1</span>; @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); <span class="rem">//地図表示</span> FragmentManager fManager = getSupportFragmentManager(); Fragment fragment = fManager.findFragmentByTag(HOGE_MAP_FRAGMENT_TAG); mapFragment = (SupportMapFragment)fragment; <span class="keyword">if</span> (mapFragment == null) { <span class="rem">//オプション</span> GoogleMapOptions gOption = <span class="keyword">new</span> GoogleMapOptions(); gOption.compassEnabled(true); gOption.rotateGesturesEnabled(true); gOption.scrollGesturesEnabled(true); gOption.tiltGesturesEnabled(true); <span class="rem">//SupportMapFragment</span> mapFragment = SupportMapFragment.newInstance(gOption); <span class="rem">//FragmentTransaction</span> FragmentTransaction fTransaction = fManager.beginTransaction(); fTransaction.add(android.R.id.content, mapFragment, HOGE_MAP_FRAGMENT_TAG); fTransaction.commit(); } } @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>); menu.add(<span class="num">0</span>, MENU_C, <span class="num">0</span>, <span class="str">"CameraInfo"</span>); <span class="keyword">return</span> true; } <span class="rem">//</span> @Override <span class="keyword">protected</span> <span class="keyword">void</span> onActivityResult(<span class="keyword">int</span> requestCode, <span class="keyword">int</span> resultCode, Intent intent) { <span class="keyword">super</span>.onActivityResult(requestCode, resultCode, intent); <span class="keyword">if</span> (requestCode == INFOACTIVITY){ <span class="keyword">if</span> (resultCode == RESULT_OK){ Bundle extras = intent.getExtras(); <span class="keyword">if</span> (extras != null){ show_toast(<span class="str">"infoActivityで入力した値:"</span> + extras.getString(<span class="str">"INFO_VALUE"</span>)); }<span class="keyword">else</span>{ show_toast(<span class="str">"infoActivityで入力した値:"</span> + <span class="str">"extra is null"</span>); } } }<span class="keyword">else</span>{ show_toast(<span class="str">"infoActivityで入力した値:"</span>); } } <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">case</span> MENU_C: <span class="rem">//</span> Intent intent = <span class="keyword">new</span> Intent(<span class="keyword">this</span>,infoActivity.<span class="keyword">class</span>); intent.putExtra(<span class="str">"MAIN_VALUE"</span>, posinfo); startActivityForResult(intent, INFOACTIVITY); <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">//GoogleMaps</span> gMap = mapFragment.getMap(); gMap.setIndoorEnabled(true); gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); gMap.setMyLocationEnabled(true); gMap.setTrafficEnabled(true); <span class="rem">//初期位置</span> LatLng target = <span class="keyword">new</span> LatLng(<span class="num">35.02878398517723</span>,<span class="num">135.77929973602295</span>); <span class="rem">//ズームレベル 1.0~19.0 無段階</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">//向き 0~360</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); uiSet.setCompassEnabled(true); uiSet.setRotateGesturesEnabled(true); <span class="rem">//イベントリスナーをセット</span> <span class="rem">//カメラ移動イベント</span> gMap.setOnCameraChangeListener(<span class="keyword">new</span> OnCameraChangeListener() { @Override <span class="keyword">public</span> <span class="keyword">void</span> onCameraChange(CameraPosition position) { LatLng point = position.target; posinfo = <span class="str">"latitude="</span> + point.latitude + <span class="str">", longitude="</span> + point.longitude; Toast.makeText(getApplicationContext(), posinfo, Toast.LENGTH_LONG).show(); } }); }<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); } } <span class="keyword">private</span> <span class="keyword">void</span> show_toast(String mes){ Toast.makeText(MainActivity.<span class="keyword">this</span>, mes, Toast.LENGTH_LONG).show(); } } |
infoActivity.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 |
<span class="keyword">import</span> android.app.Activity; <span class="keyword">import</span> android.content.Intent; <span class="keyword">import</span> android.os.Bundle; <span class="keyword">import</span> android.view.Gravity; <span class="keyword">import</span> android.view.View; <span class="keyword">import</span> android.widget.EditText; <span class="keyword">import</span> android.widget.ImageButton; <span class="keyword">import</span> android.widget.TextView; <span class="keyword">import</span> android.util.Log; <span class="rem">//</span> <span class="keyword">import</span> android.widget.LinearLayout; <span class="keyword">import</span> android.view.View.OnClickListener; <span class="keyword">import</span> android.view.WindowManager.LayoutParams; <span class="keyword">import</span> android.widget.TextView.BufferType; <span class="keyword">public</span> <span class="keyword">class</span> infoActivity <span class="keyword">extends</span> Activity { <span class="rem">//</span> <span class="keyword">private</span> EditText edittext; <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); <span class="rem">//----------------------------------</span> LinearLayout linear_LayoutL1 = <span class="keyword">new</span> LinearLayout(<span class="keyword">this</span>); linear_LayoutL1.setOrientation(LinearLayout.VERTICAL); linear_LayoutL1.setGravity(Gravity.TOP|Gravity.CENTER); addContentView(linear_LayoutL1, <span class="keyword">new</span> LayoutParams()); LinearLayout linear_LayoutL2 = <span class="keyword">new</span> LinearLayout(<span class="keyword">this</span>); linear_LayoutL2.setOrientation(LinearLayout.VERTICAL); linear_LayoutL2.setGravity(Gravity.CENTER|Gravity.CENTER); addContentView(linear_LayoutL2, <span class="keyword">new</span> LayoutParams()); LinearLayout linear_LayoutC = <span class="keyword">new</span> LinearLayout(<span class="keyword">this</span>); linear_LayoutC.setOrientation(LinearLayout.VERTICAL); linear_LayoutC.setGravity(Gravity.BOTTOM|Gravity.CENTER); addContentView(linear_LayoutC, <span class="keyword">new</span> LayoutParams()); textview = <span class="keyword">new</span> TextView(<span class="keyword">this</span>); textview.setText(<span class="str">"GPS information"</span>); linear_LayoutL1.addView(textview, <span class="keyword">new</span> LinearLayout.LayoutParams(<span class="num">350</span>,<span class="num">200</span>)); edittext = <span class="keyword">new</span> EditText(<span class="keyword">this</span>); edittext.setText(<span class="str">"info"</span>,BufferType.NORMAL); edittext.setHeight(<span class="num">50</span>); edittext.setEnabled(true); edittext.setFocusableInTouchMode(true); linear_LayoutL2.addView(edittext, <span class="keyword">new</span> LinearLayout.LayoutParams(<span class="num">300</span>,<span class="num">70</span>)); ImageButton imgbtn_A = <span class="keyword">new</span> ImageButton(<span class="keyword">this</span>); imgbtn_A.setImageResource(R.drawable.<span class="keyword">return</span>); imgbtn_A.setOnClickListener(<span class="keyword">new</span> OnClickListener(){ @Override <span class="keyword">public</span> <span class="keyword">void</span> onClick(View v) { buttonReturn_OnClickListener(v); } }); linear_LayoutC.addView(imgbtn_A, <span class="keyword">new</span> LinearLayout.LayoutParams(<span class="num">120</span>,<span class="num">120</span>)); <span class="rem">//----------------------------------</span> <span class="rem">//MainActivityから受け取った値を表示します。 </span> Intent intent = getIntent(); Bundle extras = intent.getExtras(); <span class="keyword">if</span> (extras != null){ textview.setText(<span class="str">"Mapsで取得した値:\n"</span>+extras.getString(<span class="str">"MAIN_VALUE"</span>)); }<span class="keyword">else</span>{ textview.setText(<span class="str">"Mapsで取得した値:\n"</span> + <span class="str">"extra is null"</span>); } } <span class="rem">/* * ボタンクリック処理 */</span> <span class="keyword">private</span> <span class="keyword">void</span> buttonReturn_OnClickListener(View v) { <span class="rem">//EditText textInput = (EditText)findViewById(R.id.textSub); </span> Intent intent = <span class="keyword">new</span> Intent(); intent.putExtra(<span class="str">"INFO_VALUE"</span>, edittext.getText().toString()); setResult(RESULT_OK,intent); finish(); } } |
AndroidManifest.xml
1 2 3 |
<span class="tag"><activity <span class="attr">android:name=</span><span class="value">"com.example.infoActivity"</span>></span> <span class="tag"></activity></span> |
カスタム・インフォウィンドウ
infoWindowをカスタマイズしたクラスを作って、吹き出しに画像を貼り付けてみます。
インポート
1 2 3 4 |
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter; import android.view.View; import android.widget.ImageView; import android.widget.TextView; |
Layoutに以下のようなxmlを作成
customInfoWindow.xml
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 |
<span class="tag"><?xml version="1.0" encoding="utf-8"?></span> <span class="tag"><LinearLayout <span class="attr">xmlns:android=</span><span class="value">"http://schemas.android.com/apk/res/android"</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 class="attr">android:orientation=</span><span class="value">"horizontal"</span> ></span> <span class="tag"><ImageView <span class="attr">android:id=</span><span class="value">"@+id/emblem"</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 class="attr">android:layout_marginRight=</span><span class="value">"5dp"</span> <span class="attr">android:adjustViewBounds=</span><span class="value">"true"</span> ></span> <span class="tag"></ImageView></span> <span class="tag"><LinearLayout <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> android:background="@drawable/infowin_back"//吹き出しのバック画像、不要なら削除 android:orientation="vertical" > <span class="tag"><TextView <span class="attr">android:id=</span><span class="value">"@+id/title"</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 class="attr">android:layout_gravity=</span><span class="value">"center_horizontal"</span> <span class="attr">android:ellipsize=</span><span class="value">"end"</span> <span class="attr">android:singleLine=</span><span class="value">"true"</span> <span class="attr">android:textSize=</span><span class="value">"14dp"</span> <span class="attr">android:textStyle=</span><span class="value">"bold"</span> /></span> <span class="tag"><TextView <span class="attr">android:id=</span><span class="value">"@+id/snippet"</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 class="attr">android:ellipsize=</span><span class="value">"end"</span> <span class="attr">android:singleLine=</span><span class="value">"true"</span> <span class="attr">android:textSize=</span><span class="value">"14dp"</span> /></span> <span class="tag"></LinearLayout></span> <span class="tag"></LinearLayout></span> |
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 |
<span class="keyword">private</span> <span class="keyword">class</span> CustomInfoAdapter <span class="keyword">implements</span> InfoWindowAdapter { <span class="keyword">private</span> <span class="keyword">final</span> View mWindow; <span class="rem">//constructor</span> <span class="keyword">public</span> CustomInfoAdapter() { mWindow = getLayoutInflater().inflate(R.layout.customInfoWindow, null); } @Override <span class="keyword">public</span> View getInfoWindow(Marker marker) { show_infowin(marker, mWindow); <span class="keyword">return</span> mWindow; } @Override <span class="keyword">public</span> View getInfoContents(Marker marker) { <span class="keyword">return</span> null; } <span class="keyword">private</span> <span class="keyword">void</span> show_infowin(Marker marker, View view) { <span class="rem">//タップされたMarkerをチェック</span> <span class="keyword">if</span> (marker.equals(cMarker2)) { <span class="rem">//画像は、res/drawableに入れておきます</span> ImageView emblem = (ImageView) view.findViewById(R.id.emblem); emblem.setImageResource(R.drawable.gambaemblem); <span class="rem">//タイトル</span> TextView title = (TextView) view.findViewById(R.id.title); <span class="rem">//スニペット</span> TextView snippet = (TextView) view.findViewById(R.id.snippet); <span class="rem">//setTextの内容は、マーカー作成時のoptionから引いています</span> title.setBackgroundColor(Color.BLUE); title.setText(marker.getTitle()); snippet.setTextColor(Color.BLUE); snippet.setText(marker.getSnippet()); } } } |
このクラスをマーカーにセット
gMap.setInfoWindowAdapter(new CustomInfoAdapter());
これでマーカーをタップすると、画像付きのinfoWindowが表示されます。
Leave a Reply