Android自定义InfoWindow Google Map v2上的按钮点击?

8

你好,我想在我的Android应用程序中给Google地图添加标记信息窗口。到目前为止,我已经添加了自定义信息窗口,并且它包含图片和按钮。我的目标是在点击标记后显示信息窗口,然后在用户点击窗口中的按钮后关闭信息窗口并在点击按钮后执行一些操作。但是我无法确定用户何时单击标记信息窗口中的按钮。以下是我的代码,请告诉我代码哪里出错了。

googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {

                // Use default InfoWindow frame
                @Override
                public View getInfoWindow(Marker arg0) {
                    return null;
                }

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



                    final View infoview = getLayoutInflater().inflate(R.layout.info_window,
                            null);

                    LatLng latLng = arg0.getPosition();                 

                    //tvLat.setText("Latitude:" + latLng.latitude);

                    //tvLng.setText("Longitude:" + latLng.longitude);

                     Button pickMe = (Button)infoview.findViewById(R.id.pickme);
                     pickMe.setOnClickListener(new Button.OnClickListener(){

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                Toast.makeText(getApplicationContext(), "Requested Send", Toast.LENGTH_SHORT).show();

                            }});


                    //String reverceGeoCode=new GetLocationAddress().getAddress(String.valueOf(latLng.latitude), String.valueOf(latLng.longitude));
                    //Toast.makeText(getApplicationContext(), "Rev :"+reverceGeoCode, Toast.LENGTH_LONG).show();

                    return infoview;

                }
            });

这是我的自定义信息窗口xml文件。
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"   
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Sajith Vijesekara"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:layout_marginLeft="5dp"
         android:layout_marginTop="5dp"
         android:layout_marginBottom="5dp">
         <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:src="@drawable/driver" />

    <Button
        android:id="@+id/pickme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:text="Pick Me" />

    </LinearLayout>   

</LinearLayout>

感谢Sajith。
2个回答

14
引用文档中的说明:

注意:绘制的信息窗口不是实时视图。视图在返回它时作为一张图片(使用View.draw(Canvas))呈现。这意味着以后对视图所做的任何更改都不会被地图上的信息窗口反映出来。要稍后更新信息窗口(例如,在图像加载后),请调用showInfoWindow()。此外,信息窗口将不会遵守普通视图通常具有的任何交互性,如触摸或手势事件。但是,您可以像下面的部分描述的那样侦听整个信息窗口上的通用单击事件。

因此,您无法找出某人在信息窗口中点击了Button的时间。不过,您可以在信息窗口的任何位置响应单击

非常感谢您的快速回复。如果按钮无法放在窗口中,那么有没有可能识别用户点击信息窗口本身而不是按钮时触发的事件呢? - Sajith Vijesekara
1
@Sajith4U:是的。我在最后一句话中包含了一个链接,其中有关于如何使用setOnInfoWindowClickListener()的文档说明。这里有一个演示其用法的示例项目:https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Popups - CommonsWare
谢谢,这正是我想要的。你帮我节省了时间。祝编码愉快。 - Sajith Vijesekara

8

有人编写了一个库来解决这个问题。

https://github.com/Appolica/InteractiveInfoWindowAndroid

InteractiveInfoWindowAndroid是一个Android库,它为您提供在您的Google地图上显示交互式信息窗口的机会。您的窗口的UI封装在自己的片段中,并具有自己的生命周期。您只需将其传递给InfoWindowManager,并在您想要的任何标记上方显示它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接