在Google Map Android上设置对话位置

时间:2017-07-26 17:20:16

标签: android google-maps dialog position map-projections

我想在点击标记上方实现信息窗口之类的视图。信息窗口对我没用,因为我想在其中实现多个按钮。我能够在标记点击上创建对话框,也可以设置其重力例如:GRAVITY.TOP。

我从地图的投影中获取像素中的屏幕位置。但无法在地图上设置Dialog的位置。

这是我的onMarkerClick方法:

@Override
    public boolean onMarkerClick(Marker arg0) {

        Log.d(TAG, "Marker Clicked"+ arg0.getTitle());

        final Dialog d = new Dialog(this);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        d.setContentView(R.layout.info_window_layout);

        AppCompatImageView btnAction1 = d.findViewById(R.id.action1);

        AppCompatImageView btnAction2 = d.findViewById(R.id.action2);
        AppCompatImageView btnAction3 = d.findViewById(R.id.action3);

        final LatLng latlong = arg0.getPosition();

        Projection projection = mMap.getProjection();

        // Returns a screen location that corresponds to a geographical coordinate
        Point screenPosition = projection.toScreenLocation(latlong);

        Window window = d.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.x = screenPosition.x;
        wlp.y = screenPosition.y;

        wlp.gravity = Gravity.NO_GRAVITY;

        wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wlp);   

        d.show();
        return true;
    }

我的info_window_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/action1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_action1"/>

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/action2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_action2"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"/>

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/action3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_actione3"/>

</LinearLayout>

这是截图,我现在得到了什么!

Screenshot

0 个答案:

没有答案