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

Ebo*_*ike 5 android google-maps android-layout

我在GoogleMap中有一个带有按钮的InfoWindow,我正在尝试使用choose007的hack变种来让它们工作(请参阅此问题).

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

但是,所有获取屏幕坐标的尝试都会失败.我在里面调用它onDispatchTouchEvent(即在创建它之后很久),但是按钮和视图都得到0,0.

我假设这是因为InfoWindow是某种特殊情况而不是实际的UI元素?(IIRC整个视图基本上只是一个位图在地图顶部的位图?)有没有办法获得坐标?我应该提一下,我试图避免涉及魔术数字的任何事情,比如在链接中选择了0000的解决方案.

这是我正在做的事情的简化说明:

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));
}
Run Code Online (Sandbox Code Playgroud)

dan*_*117 1

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

如果您希望在用户单击标记时显示自定义内容,然后捕获标记,请单击在标记坐标中心显示您的视图,地图到标记坐标并true onMarkerClick

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

public boolean onMarkerClick (Marker marker)
Run Code Online (Sandbox Code Playgroud)

单击或点击标记时调用。参数 标记 单击的标记。退货

如果侦听器已使用该事件,则为 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>
Run Code Online (Sandbox Code Playgroud)

就我个人而言,当单击信息窗口时,我只会在整个屏幕上显示豪华的透明覆盖层。我的意思是,无论哪种方式,用户都必须单击某些内容。

祝你好运,在 infoWindow 侦听器之前签署一键点击。