android - 如何获取 map 信息窗口 View 的屏幕坐标

标签 android google-maps android-layout

我在 GoogleMap 中有一个带有按钮的信息窗口,我正在尝试使用 chose007 的 hack 变体来使它们正常工作(参见 question)。

我的方法依赖于获取 InfoWindow 中按钮的屏幕坐标并在 onDispatchTouchEvent 函数中对其进行操作。

但是,所有获取屏幕坐标的尝试都失败了。我在 onDispatchTouchEvent 中调用它(即在它创建很久之后),但是按钮和 View 都得到 0,0。

我认为这是因为 InfoWindow 是某种特殊情况而不是实际的 UI 元素? (IIRC 整个 View 基本上只是一个在 map 顶部被 blitted 的位图?)有没有办法获得坐标?我应该提一下,我正在努力避免任何涉及魔数(Magic Number)的事情,例如链接中 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));
}

最佳答案

所以您在信息窗口上遇到了死胡同,因为您不应该使用它来制作具有按钮等的真正自定义信息窗口。不是这个洋基工程师不会那样做。

如果您希望在用户单击标记时显示自定义内容,则捕获标记单击,在标记坐标中心显示您的 View , map 到标记坐标并返回 true onMarkerClick

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

public boolean onMarkerClick (Marker marker)

在单击或点击标记时调用。 参数 marker 被点击的标记。 返回

如果监听器已经使用事件(即不应发生默认行为),则为 true,否则为 false(即,应发生默认行为)。默认行为是相机移动到 map 并显示信息窗口。

那么我如何在 map 上放置信息窗口形状的东西呢?我需要一张图片或一些花哨的线条艺术,而这些在我努力剪切和粘贴的源代码中并不容易获得.

那么如何在 map 上放置一个按钮。好吧,我可以做到,但对于这个例子,按钮位于静态位置。伪信息窗口你必须将按钮或图像移动到标记的坐标,并且可能将 map 居中在标记坐标上。

<?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>

我个人只想在单击信息窗口时在整个屏幕上显示一个豪华的透明覆盖层。我的意思是用户必须点击任何方式。

祝你好运 签 在 infoWindow 监听器之前单击一下。

关于android - 如何获取 map 信息窗口 View 的屏幕坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23852747/

相关文章:

java - import com.android.internal.util.BuildInfo 无法解析

android - 禁用演示文稿的隐藏(辅助屏幕)

javascript - 谷歌地图天空 API v3

android - 使用 Canvas (surfaceview) 或使用带有平移动画的 View

java - 如何使用 FusedLocation API 每 5 分钟接收一次位置更新

android - 仅当用户更改时更改 Android 上的 Wifi 状态

ios - 无法获取 Google Maps iOS SDK 的我的位置点

javascript - 如何在每 1 分钟加载后保持我在 map 中的最后位置

android - 如何限制 GridLayout 中 ListView 的高度?

java - ArrayAdapter要求资源id是listview Android Studio中的textview