【问题标题】:Android custom InfoWindow Google Map v2 onclick button?Android自定义InfoWindow Google Map v2 onclick按钮?
【发布时间】:2014-07-31 11:48:27
【问题描述】:

嗨,我想在 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;

                }
            });

这是我的 customInfo-window 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>

谢谢 萨吉特

【问题讨论】:

    标签: android google-maps onclick customization infowindow


    【解决方案1】:

    引用the documentation:

    注意:绘制的信息窗口不是实时视图。视图在返回时呈现为图像(使用 View.draw(Canvas))。这意味着对视图的任何后续更改都不会反映在地图上的信息窗口中。要稍后更新信息窗口(例如,加载图像后),请调用 showInfoWindow()。此外,信息窗口不会尊重任何普通视图的典型交互性,例如触摸或手势事件。但是,您可以在整个信息窗口上收听通用点击事件,如下节所述。

    因此,您无法知道何时有人在您的信息窗口中点击了Button。不过,欢迎您使用respond to clicks anywhere on the info window

    【讨论】:

    • 感谢您的快速响应。因此,如果按钮无法放入窗口。当用户单击信息窗口时,是否可以识别事件,而不是按钮。
    • @Sajith4U:是的。我在最后一句中包含了一个文档链接,该链接解释了如何为此使用setOnInfoWindowClickListener()。这是一个演示其用法的示例项目:github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Popups
    • 谢谢这是我想要的。你节省了我的时间。快乐的编码。
    【解决方案2】:

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

    https://github.com/Appolica/InteractiveInfoWindowAndroid

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

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 2013-08-26
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      相关资源
      最近更新 更多