在标记Android上弹出或布局

红死神II

我想在标记上放一个自定义布局。到目前为止,我得到了:

default_marker_info_window.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/markerInfoContainer"
        android:background="@android:color/holo_red_dark"
        android:maxWidth="250dp">
    </ViewFlipper>    

    <ImageView
        android:layout_width="20dp"
        android:layout_height="15dp"
        android:background="@drawable/ic_down_arrow"
        android:layout_gravity="center|top"/>

</LinearLayout>

default_marker_info_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TextView
            android:id="@+id/tvTitulo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:gravity="center"/>

        <TextView
            android:id="@+id/tvCuerpo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="press me 1"
            android:id="@+id/button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="press me 2"
            android:id="@+id/button2"/>

    </LinearLayout>

</LinearLayout>

MapActivity.java

public class MapActivity extends Activity implements GoogleMap.OnMapLongClickListener{

    protected GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        setUpMapIfNeeded();
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {

                View mainView = getLayoutInflater().inflate(R.layout.default_marker_info_window, null);
                ViewFlipper markerInfoContainer = (ViewFlipper)mainView.findViewById(R.id.markerInfoContainer);
                View viewContainer = getLayoutInflater().inflate(R.layout.default_marker_info_layout, null);
                TextView tvTitulo = (TextView) viewContainer.findViewById(R.id.tvTitulo);
                TextView tvCuerpo = (TextView) viewContainer.findViewById(R.id.tvCuerpo);
                tvTitulo.setText(marker.getTitle());
                tvCuerpo.setVisibility(View.GONE);

                markerInfoContainer.addView(viewContainer);

                PopupWindow popupWindow = new PopupWindow(mainView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.showAtLocation(findViewById(R.id.map), Gravity.CENTER_HORIZONTAL, 0, 0); //map is the fragment on the activity layout where I put the map   

                return true;
            }
        });
        ...
    }
...
}

但是我有几个问题。

问题1:弹出式窗口显示(隐藏)标记(3D)。我想将其放置在它上面(2D),像这样:

我想要的弹出窗口的位置

可通过帮助xy标记的位置,并使用它们popupWindow.showAtLocation但是,如果您还有其他解决方案,我会听他们说。

问题2:当我在地图上滑动时,布局保持在屏幕中央,但我想将其保持在标记上方。

在地图上滑动时出现问题

注意:我无法使用,mMap.setInfoWindowAdapter因为我会丢失按钮的点击侦听器。

Stas parshin

定义一些变量以保存上次使用的标记和popupView

private Marker mMarker;
private PopupWindow mPopupWindow;
private int mWidth;
private int mHeight;

在我们的onMarkerClick中:

@Override
public boolean onMarkerClick(Marker marker) {
    if (mPopupWindow != null) {
        mPopupWindow.dismiss();
    }
    View popupView;// inflate our view here
    PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    popupView.measure(size.x, size.y);

    mWidth = popupView.getMeasuredWidth();
    mHeight = mPopupView.getMeasuredHeight();
    mMarker = marker;
    mPopupWindow = popupWindow;

    updatePopup();

    return true;
}

updatePopup方法:

private void updatePopup() {
    if (mMarker != null && mPopupWindow != null) {
        // marker is visible
        if (mMap.getProjection().getVisibleRegion().latLngBounds.contains(mMarker.getPosition())) {
            if (!mPopupWindow.isShowing()) {
                mPopupWindow.showAtLocation(getView(), Gravity.NO_GRAVITY, 0, 0);
            }
            Point p = mMap.getProjection().toScreenLocation(mMarker.getPosition());
            mPopupWindow.update(p.x - mPopupWidth / 2, p.y - mPopupHeight + 100, -1, -1);
        } else { // marker outside screen
            mPopupWindow.dismiss();
        }
    }
}

我们需要CameraChangeListener:

mMap.setOnCameraChangeListener(this);

@Override
public void onCameraChange(CameraPosition cameraPosition) {
    ...
    updatePopup();
}

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Yii Booster在文本而不是按钮上弹出

来自分类Dev

视频无法在引导模式下在Firefox上弹出

来自分类Dev

在视图上弹出文档内容?

来自分类Dev

在VB中的selectindexchanged上弹出消息

来自分类Dev

关闭后退按钮上弹出

来自分类Dev

Android布局:键盘弹出时按钮会移动

来自分类Dev

在Angularjs中的mouseover上弹出一个div

来自分类Dev

在mouseover div上弹出消息

来自分类Dev

如何在其他Android应用程序上弹出气泡?

来自分类Dev

运行程序时在Visual Studio上弹出命令窗口

来自分类Dev

如何防止键盘在移动设备上弹出?

来自分类Dev

在Tabclick上弹出根目录,在Android上为FreshTabbedNavigationContainer

来自分类Dev

在粘性组件上弹出

来自分类Dev

在TOuch上弹出gridview单元

来自分类Dev

如何更改Android溢出按钮上弹出菜单的位置?

来自分类Dev

使用多个信标(在每个不同的信标上弹出视图)

来自分类Dev

Google Map-每3秒在随机标记上弹出一次信息窗口

来自分类Dev

AUI datepicker:在元素焦点上弹出datepicker

来自分类Dev

在Excel单元格上弹出消息

来自分类Dev

从cron在GUI上弹出一条消息

来自分类Dev

如何在Ruby中的PG :: Result上弹出?

来自分类Dev

如何在其他Android应用程序上弹出气泡?

来自分类Dev

删除标签上弹出框的编码

来自分类Dev

如何防止键盘在移动设备上弹出?

来自分类Dev

javascript 警报不会在 php echo 上弹出

来自分类Dev

使用javascript单击提交后在空输入上弹出

来自分类Dev

在主屏幕上弹出

来自分类Dev

在 Ubuntu 20.04 上弹出对话框非常慢

来自分类Dev

Openlayers 在地图上弹出 autoPan 并带有填充