标记Android上的弹出或布局

时间:2015-08-13 13:58:53

标签: android google-maps layout

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

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),像这样:

Position of popup I want

可以在标记的xy位置的帮助下,并在popupWindow.showAtLocation上使用它们。但如果你有其他解决方案,我会听取他们的意见。

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

Problem while sliding on map

注意:我无法使用mMap.setInfoWindowAdapter,因为我将失去按钮的点击听众。

1 个答案:

答案 0 :(得分:2)

定义一些变量以保存上次使用的标记和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();
}