android - 显示 infowindow android map

标签 android google-maps

如何在加载 map 时显示标记信息窗口?当我点击 map 名称时,一个新的 Activity 在 map 加载的地方开始。它显示标记,问题是我想自动显示我创建的自定义信息窗口。下面是我的代码:我会为那些可以修复的人标记这个答案这。谢谢。

if (nameList.get(i).equals(selected)){

            Marker marker = map.addMarker(new MarkerOptions()
             .title(nameList.get(i))
             .snippet(addressList.get(i))
             .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_location))
             .position(new LatLng(latList.get(i), lngList.get(i))));

            marker.showInfoWindow();

            //Set Custom InfoWindow
            map.setInfoWindowAdapter(new InfoWindowAdapter() {


            @Override
            public View getInfoWindow(Marker arg0) {



                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {

                View myContentsView = getLayoutInflater().inflate(R.layout.map_info, null);

                TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
                tvTitle.setText(marker.getTitle());

                TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
                tvSnippet.setText(marker.getSnippet());

                TextView infoSnippet = (TextView)myContentsView.findViewById(R.id.moreinfo);
                infoSnippet.setText("Click for more details.");

                return myContentsView;
            }
        });

            //Open New Activity
            map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker marker) {

                Intent intent = new Intent(SelectedPlaceActivity.this, PlaceDescriptionActivity.class);
                intent.putExtra("placetitle", marker.getTitle().toString());
                intent.putExtra("snippet", marker.getSnippet().toString());
                startActivity(intent);

                }
            });


        }

最佳答案

看来您需要一个InfoWindow 适配器。请参阅以下示例:

map.setInfoWindowAdapter(new InfoWindowAdapter(){
    // Use default InfoWindow frame
    @Override
    public View getInfoWindow(Marker marker) {              
        return null;
    }           

    // Defines the contents of the InfoWindow
    @Override
    public View getInfoContents(Marker marker) {

        // Getting view from the layout file info_window_layout
        View v = getLayoutInflater().inflate(R.layout.custom_info_window, null);

        // Getting reference to the TextView to set title
        TextView note = (TextView) v.findViewById(R.id.note);
        note.setText(marker.getTitle() );

        return v;
    }
});

如您所见,您需要创建自定义布局。在我的示例中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView android:id="@+id/note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textColor="#000000"/>

    <ImageButton android:id="@+id/takeMeThere"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/note"
        android:contentDescription="@string/directions"
        android:src="@drawable/ic_action_directions"
        android:background="@null"/>
</RelativeLayout>

希望对你有帮助

关于android - 显示 infowindow android map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25007041/

相关文章:

java - 从游戏结束屏幕返回到主要 Activity

android - 根据字符串列选择不同的第一个字符

javascript - 使用 Google map 在 iPad 上缩放页面

java - 随着 map 的移动在谷歌地图上获取标记

java - 来自 fragment 内部的 setText

c# - 更改文本选择颜色(强调色)Xamarin Android

android - ViewModelProvider.NewInstanceFactory - 接收相同的实例

android - Android 手机标配 Google map ?

iphone - 从 google places API 获取数据后获取状态 "REQUEST_DENIED"

android - Android 2.2 中的 GOOGlE API V2 问题