如何获取地图InfoWindow视图的屏幕坐标

5
我在GoogleMap中有一个InfoWindow,其中包含按钮,并尝试使用chose007的hack变体使其正常工作(请参见此question)。
我的方法依赖于获取InfoWindow内部按钮的屏幕坐标,并在onDispatchTouchEvent函数中对其进行操作。
但是,所有获取屏幕坐标的尝试都失败了。我在onDispatchTouchEvent内调用它(即在创建后很长时间),但是我得到的是按钮和视图的0,0坐标。
我认为这是因为InfoWindow是某种特殊情况而不是实际的UI元素?(如果我没记错,整个视图基本上只是一个被位于地图顶部的位图复制的东西?)有没有办法获取坐标?我应该提到,我正在尝试避免任何涉及魔术数字的事情,例如链接中chose007的解决方案。
这是我正在做的简化说明:
public boolean onDispatchTouchEvent(MotionEvent ev) {
    int[] location = new int[2];
    mTestButton.getLocationOnScreen(location);  // Returns 0,0
    mTestView.getLocationOnScreen(location);    // Returns 0,0

    return false;
}

public View getInfoContents(Marker marker) {
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mTestView = inflater.inflate(R.layout.info_window, null);
    mTestButton = ((Button) mTestView.findViewById(R.id.test_button));
}

1
你的假设是正确的,信息窗口只是绘制的快照,不是View层次结构的一部分。获取它们的定位的唯一方法是通过chose007答案中的“魔法数字”,该数字从Marker的位置和信息窗口的尺寸计算出来,假设默认锚定和旋转。我还推导出了一个更通用的实现,允许自定义锚定和旋转,并使信息窗口保持活动状态,而无需通过触摸监听器进行修改,这些都在答案的评论中进行了讨论。 - corsair992
尝试显示自己的PopupWindow,将其锚定到标记坐标。 - S.D.
1个回答

1

如果您想要制作真正自定义的信息窗口,例如带有按钮之类的内容,那么您已经走进了一个死胡同,因为这不是应该使用的方法。这位美国工程师也不会这样做。

如果您想要在用户单击标记时显示自定义内容,则可以捕获标记点击事件,显示您的视图并将地图中心设置为标记坐标,并从onMarkerClick返回true

https://developers.google.com/maps/documentation/android/marker#marker_click_events

public boolean onMarkerClick (Marker marker)

当标记被点击或轻触时调用。 参数 marker 被点击的标记。 返回值

如果监听器已消耗事件(即不应发生默认行为),则返回true;否则返回false(即默认行为应该发生)。默认行为是将相机移动到地图并显示信息窗口。

那么如何在地图上放置一个类似信息窗口的东西呢?我需要一张图片或一些花哨的线条艺术品,但这不是我很容易在源代码中找到并复制粘贴的东西。

那么如何在地图上放置一个按钮呢?对于这个例子,我可以做到,但按钮的位置是静态的。伪信息窗口,您将不得不将按钮或图像移动到标记的坐标,并可能将地图居中于标记坐标。

<?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="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="34dp" >

        <TextView
            android:id="@+id/tvSpeed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="55sp" />

        <TextView
            android:id="@+id/tvSpeedUnits"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="21sp" />
    </LinearLayout>

    <Button
        android:id="@+id/btnFollow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="34dp"
        android:background="@drawable/mapbutton"
        android:padding="14dp"
        android:text="@string/btnFollowText"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/tvSatLock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="sat lock"
        android:textColor="@color/black"
        android:textSize="21sp" />

    <Button
        android:id="@+id/btnGPS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/mapbutton"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:text="Enable GPS"
        android:textColor="@color/black" />

</RelativeLayout>

个人而言,当信息窗口被点击时,我会简单地在整个屏幕上显示一个豪华的透明覆盖层。我的意思是,无论你选择哪种方式,用户都必须点击某些东西。

祝好运 签名 比信息窗口监听器更加高效的一次点击。


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