2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

google maps for AndroidのinfoWindowをカスタマイズする

Last updated at Posted at 2019-03-13

#0.実行例
スクリーンショット 2019-03-13 11.47.17.png

#1.setInfoWindowAdapter()
GoogleMap.setInfoWindowAdapter()で、InfoWindowAdapterインタフェースを実装したオブジェクトを設定する(getInfoWindow()またはgetContents()を実装する)。

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
//〜略〜
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;    //どちらかに処理を記述
            }

            @Override
            public View getInfoContents(Marker marker) {
                return null;    //どちらかに処理を記述
            }

#2.InfoWindowのレイアウトを記述
res/layout/に新しくレイアウトリソースファイルを作成し、infoWindowのレイアウトを記述する。
res/values/strings.xmlも必要に応じて記述を加える。

info_window_ayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/iw_1"/>
    <TextView
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/iw_2"/>
    <TextView
        android:id="@+id/tv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/iw_3"/>
    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bt_1"/>

</LinearLayout>
strings.xml
<resources>
<!-- 〜略〜 -->
    <string name="iw_1">1行目の情報</string>
    <string name="iw_2">2行目の情報</string>
    <string name="iw_3">3行目の情報</string>
    <string name="bt_1">ボタンも置ける</string>
</resources>

#3.getInfoContents()の実装

            @Override
            public View getInfoContents(Marker marker) {
                View view = getLayoutInflater().inflate(R.layout.info_window_layout,null);
                return view;
            }

#参考
インフォウィンドウをカスタマイズする
[Info Windows | Maps SDK for Android | Google Developers]
(https://developers.google.com/maps/documentation/android-sdk/infowindows#custom_info_windows)

2
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?