为什么我无法访问添加到添加到 Google 地图中标记的自定义信息窗口的按钮?

查尔斯

我正在创建一个小应用程序,可以帮助用户找到一个体育俱乐部。根据之前活动中从旋转器中选择的运动类型,地图上将显示一些代表不同俱乐部的标记。(见下面的截图)

在微调器中单击某种运动后的结果

我试图在 Google 地图中将开始另一个活动的标记的信息窗口(包含标题和片段)中添加一个按钮。它现在会弹出一个吐司。目前我正在产生以下结果:

应用截图(无法嵌入图片,因为信誉太低)

我遇到的问题是,当我尝试单击按钮时,我无法与信息窗口中的按钮交互,而是单击了整个信息窗口(导致窗口从白色背景变为灰色背景以显示它已被单击)。无论我尝试单击按钮还是信息窗口的任何其他部分,都会发生这种情况。

灰色背景表示已单击信息窗口

为此,我创建了一个 XML 布局文件(custom_window_info.xml),如果单击标记,该文件将显示在地图活动中。它显示在MapActivity.java 类中声明的标题、片段和按钮方法

然后我创建了一个 java 类(CustomInfoWindowAdapter.java),它为上面提到的布局文件提供了 java 功能,例如将值分配给标题和片段文本字段、getInfoWindow() 和 getInfoContents() 方法等。

我不确定我是否完全以错误的方式处理了这个问题,或者我犯了一个小错误。任何提示/建议?

custom_window_info.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/title"
                android:layout_gravity="center_horizontal"
                android:ellipsize="end"
                android:maxLines="1"
                android:textColor="#000"
                android:textSize="14sp"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:id="@+id/snippet"
                android:maxLines="5" />

            <Button
                android:id="@+id/joinCLub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:onClick="ClubButtonClick"
                android:text="Join Club"
                android:visibility="visible" />
        </LinearLayout>
    </LinearLayout>

自定义信息窗口适配器.java

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

private final View mWindow;
private Context mContext;

public CustomInfoWindowAdapter(Context context) {
    this.mContext = mContext;
    this.mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window, null);
}

private void rendowWindowText(Marker marker, View v){

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

    if(!title.equals(" ")){
        clubtitle.setText(title);
    }

    String snippet = marker.getSnippet();
    TextView clubsnippet = (TextView) v.findViewById(R.id.snippet);

    if(!title.equals(" ")){
        clubsnippet.setText(snippet);
    }
}

@Override
public View getInfoWindow(Marker marker) {
    rendowWindowText(marker, mWindow);
    return mWindow;
}

@Override
public View getInfoContents(Marker marker) {
    rendowWindowText(marker, mWindow);
    return mWindow;
}


  }

地图活动

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

  private GoogleMap mMap;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
  }


  @Override
  public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Assigning coordinates to a club (I've left out the 4 other markers to reduce the code)
    LatLng vincentsGAA = new LatLng(53.374042,-6.2300634);

    // Adding marker to map with the coordinates declared above, a title for the name of the club and a snippet for the address  
    mMap.addMarker(new MarkerOptions().position(vincentsGAA).title("St Vincents GFC").snippet("Malahide Road, Dublin"));

    //Assigning the default coordinates to Dublin, Ireland when the map opens
    LatLng dublin = new LatLng(53.3242381,-6.385785);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dublin, 10));

    // Sets the custom info window to this map activity's markers
    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MapsActivity.this));
  }


  // This is the back button in the top right of the map activity that will 
  // bring the user back to the previous activity that had the spinner that 
  // allows the user to pick the type of sport they want to see clubs for
  public void ButtonClick(View v) {


     Intent intent1 = new Intent(this, Welcome_Activity.class);
     startActivity(intent1);

  }
}
怪异的阿里

实际上,这是不可能的,Google Map 会将您自定义的 InfoWindow 的内容呈现为图像并将其作为图像显示给您。因此,您仅将单击侦听器设置为整个窗口,而不是其中的视图。

您唯一的选择是将 ClickListener 设置为整个 InfoWindow 并弹出一个包含您想要的可点击内容的 Dialog,而不是直接在 InfoWindow 内进行。

来自 Google 文档:https : //developers.google.com/maps/documentation/android/marker#info_windows

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android-Google Maps V2-将自定义信息窗口添加到从数组创建的标记中

来自分类Dev

无法设置Google地图的自定义信息窗口

来自分类Dev

将标记添加到Google API

来自分类Dev

为JSON地图中的数据设置Google Maps标记的自定义信息窗口

来自分类Dev

为JSON地图中的数据设置Google Maps标记的自定义信息窗口

来自分类Dev

在Google地图中将本地图像添加为自定义标记

来自分类Dev

将自定义链接添加到Google地图的文字说明路线中

来自分类Dev

使用 javascript 将自定义位置图钉添加到样式化的 Google 地图

来自分类Dev

将自定义标记文字添加到Google Map

来自分类Dev

如何将自定义数据添加到标记(Google Maps API SWIFT)

来自分类Dev

导航地图时将标记添加到Google Map

来自分类Dev

将图像添加到多个Google Map信息窗口

来自分类Dev

将Google图表添加到FusionTableLayer信息窗口

来自分类Dev

在Android上将自定义页脚按钮添加到Google Maps Marker

来自分类Dev

带有kml图层的Google地图。Google将_blank添加到信息窗口链接

来自分类Dev

带有kml图层的Google地图。Google将_blank添加到信息窗口链接

来自分类Dev

如何将自定义Google搜索添加到我的自定义搜索框中?

来自分类Dev

如何将自定义Google搜索添加到我的自定义搜索框中?

来自分类Dev

自定义图像以添加到地图标记

来自分类Dev

如何将信息窗口添加到 Google 地图 Api 中的数据层

来自分类Dev

无法添加到地图

来自分类Dev

如何将业务添加到Google地图

来自分类Dev

将预设添加到ACF位置字段(Google地图)

来自分类Dev

将预设添加到ACF位置字段(Google地图)

来自分类Dev

尝试将KML图层添加到Google地图

来自分类Dev

将标记添加到动态Google Map

来自分类Dev

将可点击的链接添加到Google Map标记

来自分类Dev

将图钉(非标记)添加到Google Map Android

来自分类Dev

Android Google Map API将标题添加到我的标记中

Related 相关文章

  1. 1

    Android-Google Maps V2-将自定义信息窗口添加到从数组创建的标记中

  2. 2

    无法设置Google地图的自定义信息窗口

  3. 3

    将标记添加到Google API

  4. 4

    为JSON地图中的数据设置Google Maps标记的自定义信息窗口

  5. 5

    为JSON地图中的数据设置Google Maps标记的自定义信息窗口

  6. 6

    在Google地图中将本地图像添加为自定义标记

  7. 7

    将自定义链接添加到Google地图的文字说明路线中

  8. 8

    使用 javascript 将自定义位置图钉添加到样式化的 Google 地图

  9. 9

    将自定义标记文字添加到Google Map

  10. 10

    如何将自定义数据添加到标记(Google Maps API SWIFT)

  11. 11

    导航地图时将标记添加到Google Map

  12. 12

    将图像添加到多个Google Map信息窗口

  13. 13

    将Google图表添加到FusionTableLayer信息窗口

  14. 14

    在Android上将自定义页脚按钮添加到Google Maps Marker

  15. 15

    带有kml图层的Google地图。Google将_blank添加到信息窗口链接

  16. 16

    带有kml图层的Google地图。Google将_blank添加到信息窗口链接

  17. 17

    如何将自定义Google搜索添加到我的自定义搜索框中?

  18. 18

    如何将自定义Google搜索添加到我的自定义搜索框中?

  19. 19

    自定义图像以添加到地图标记

  20. 20

    如何将信息窗口添加到 Google 地图 Api 中的数据层

  21. 21

    无法添加到地图

  22. 22

    如何将业务添加到Google地图

  23. 23

    将预设添加到ACF位置字段(Google地图)

  24. 24

    将预设添加到ACF位置字段(Google地图)

  25. 25

    尝试将KML图层添加到Google地图

  26. 26

    将标记添加到动态Google Map

  27. 27

    将可点击的链接添加到Google Map标记

  28. 28

    将图钉(非标记)添加到Google Map Android

  29. 29

    Android Google Map API将标题添加到我的标记中

热门标签

归档