【问题标题】:Custom infowindow with google maps api v2带有谷歌地图 api v2 的自定义信息窗口
【发布时间】:2014-01-23 17:46:54
【问题描述】:

我可以用一段简单的代码自定义 infoWindow 的内容:

 private GoogleMap mMap;
 mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker arg0) {

            View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
            return v;

        }
    });

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="HELLO"
        android:textColor="#FF0000"/>

</LinearLayout>

但这只会改变内容,不会改变 infoWindow 本身。它仍然保持白色,底部有一个小阴影,现在可以看到带有黑色背景的红色 HELLO 文本。我想将此默认 infoWindow 更改为透明的黑色矩形。我该怎么做?

【问题讨论】:

标签: android google-maps-android-api-2


【解决方案1】:

您必须在 getInfoWindow 方法中编写代码。因此您可以自定义信息窗口。 例如

@Override
public View getInfoWindow(Marker marker) {

    // Getting view from the layout file
    View v = inflater.inflate(R.layout.map_popup, null);

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

    TextView address = (TextView) v.findViewById(R.id.distance);
    address.setText(marker.getSnippet());

    return v;
}

@Override
public View getInfoContents(Marker arg0) {
    // TODO Auto-generated method stub
    return null;
}

【讨论】:

  • 这是正确的答案,必须使用getInfoWindow而不是使用默认的白框。
  • 您需要在 map.addMarker() 方法之后立即 setInfoWindowAdapter() 才能生效。不要在 onMarkerClick() 中调用 setInfoWindowAdapter()。
  • 我建议在调用函数之前先膨胀视图,并且仅在调用 getInfoWindow() 时更改相关信息
【解决方案2】:

这样用……

myMap.setInfoWindowAdapter(new InfoWindowAdapter() {

                @Override
                public View getInfoWindow(Marker arg0) {

                    ContextThemeWrapper cw = new ContextThemeWrapper(
                            getApplicationContext(), R.style.Transparent);
                    // AlertDialog.Builder b = new AlertDialog.Builder(cw);
                    LayoutInflater inflater = (LayoutInflater) cw
                            .getSystemService(LAYOUT_INFLATER_SERVICE);
                    View layout = inflater.inflate(R.layout.custom_infowindow,
                            null);
                    return layout;
                }

                @Override
                public View getInfoContents(Marker arg0) {

                    return null;

                }
            });

R.style.Transparent 在你的 style.xml 中添加这个

<style name="Transparent" parent="android:Theme.Light">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

    <color name="transparent">#00000000</color>

已编辑:

custom_infowindow.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF0000"
        android:text="HELLO" />

</LinearLayout>

【讨论】:

  • 这不会改变任何事情。我得到与以前相同的输出。但是,我看到它对你有用。我删除了所有样式但徒劳无功...
  • @erdomester:我在我的答案中添加了 custom_infowindow.xml .. 只需使用我所有的代码插入你的...并告诉我它是否有效..
  • 我仍然得到带有红色 HELLO 的默认布局:2shared.com/photo/lBOzOHMz/hello.html 我正在测试 2.3.5
  • @Dhawal Sodha Parmar 你的回答很好非常感谢你。但是你能告诉我这两种方法之间的区别吗:1。 public View getInfoWindow(Marker arg0) {} 和 2. public View getInfoContents(Marker arg0) {}
【解决方案3】:

试试这个...我对你的代码做了一点改动。

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

   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="#80000000" 
    >

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:text="HELLO"
     android:textColor="#FF0000"
     android:padding="10dp"/>

   </LinearLayout> 

</LinearLayout>

也改变这个...

 mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        public View getInfoWindow(Marker arg0) {
            View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
            return v;
        }

        public View getInfoContents(Marker arg0) {

            //View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);

            return null;

        }
    });

我希望它会起作用...:)

【讨论】:

  • @erdomester-我的代码会显示透明的黑色矩形。我有测试。你测试了吗?
  • 我当然测试过了。我什至从 styles.xml 中删除了所有样式。
  • @erdomester-好的谢谢你的回复...可能还有其他问题..!
【解决方案4】:

您需要在 map.addMarker() 方法之后立即 setInfoWindowAdapter() 才能使其生效。不要在 onMarkerClick() 中调用 setInfoWindowAdapter()。

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    相关资源
    最近更新 更多