Osmdroid:如何在标记旁边创建上下文菜单?

Osmdroid: How to create a Contextmenu next to a Marker?

我的目标:我想在 osmdroid-marker 旁边显示一个弹出菜单

我的问题:

目前我正在将菜单附加到位于右下角但可能远离当前标记的图像。

这是我当前的代码(来自 https://github.com/k3b/AndroFotoFinder/blob/FDroid/app/src/main/java/de/k3b/android/androFotoFinder/locationmap/LocationMapFragment.java

public class LocationMapFragment extends DialogFragment {
    ...
    protected   boolean showContextMenu(final View parent, final int markerId,
                                        final IGeoPoint geoPosition, final Object markerData) {
        MenuInflater inflater = getActivity().getMenuInflater();

        // currently the contextmenu is attached to some image "this.mImage" 
        // which does not correspont to "geoPosition"
        // How to create a temporary 1x1 view at "geoPosition" 
        // where i can attach the menu to?
        PopupMenu menu = new PopupMenu(getActivity(), this.mImage);

        inflater.inflate(R.menu.menu_map_context, menu.getMenu());

        menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.cmd_gallery:
                        return showGallery(getiGeoPointById(markerId, geoPosition));
                    case R.id.cmd_zoom:
                        return zoomToFit(getiGeoPointById(markerId, geoPosition));
                    default:
                        return false;
                }
            }
        });
        menu.show();
        return true;
    }
}

我的问题:有没有一种简单的方法可以在我的 IGeoPoint geoPosition 标记顶部创建一个临时的 1x1 像素 window,我可以将弹出窗口附加到该位置?

或者有其他简单的方法来定位菜单吗?

使用 OSMBonusPack Marker InfoWindow(标记气泡)。

你可以用infowindow.getView()得到它的视图,这是一个普通的android.view.View。

您可以根据需要设计其布局以使其尽可能小。

就像MKer说的,你可以使用OSMBonusPack Marker InfoWindow,定制你自己的InfoWindow。 https://mobiledevstories.wordpress.com/2014/03/01/osmdroid-bonus-pack-markers-with-clickable-infowindows/

这是我的最终解决方案。 createTempPopupParentMenuView(GeoPoint position) 包含从 InfoWindow 中提取的代码,为位于标记位置的弹出菜单提供视图:

/** temporary 1x1 pix view where popup-menu is attached to */
private View tempPopupMenuParentView = null;

...

protected   boolean showContextMenu(final View parent, final int markerId,
                                    final IGeoPoint geoPosition, final Object markerData) {
    MenuInflater inflater = getActivity().getMenuInflater();
    mMapView.removeView(tempPopupMenuParentView);

    PopupMenu menu = new PopupMenu(getActivity(), createTempPopupParentMenuView(new GeoPoint(geoPosition.getLatitudeE6(), geoPosition.getLongitudeE6())));

    inflater.inflate(R.menu.menu_map_context, menu.getMenu());

    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            mMapView.removeView(tempPopupMenuParentView);

            switch (item.getItemId()) {
                case R.id.cmd_gallery:
                    return showGallery(getiGeoPointById(markerId, geoPosition));
                case R.id.cmd_zoom:
                    return zoomToFit(getiGeoPointById(markerId, geoPosition));
                default:
                    return false;
            }
        }
    });
    menu.show();
    return true;
}

// inspired by org.osmdroid.bonuspack.overlays.InfoWindow
private View createTempPopupParentMenuView(GeoPoint position) {
    if (tempPopupMenuParentView != null) mMapView.removeView(tempPopupMenuParentView);
    tempPopupMenuParentView = new View(getActivity());
    MapView.LayoutParams lp = new MapView.LayoutParams(
            1,
            1,
            position, MapView.LayoutParams.CENTER,
            0, 0);
    tempPopupMenuParentView.setVisibility(View.VISIBLE);
    mMapView.addView(tempPopupMenuParentView, lp);
    return tempPopupMenuParentView;
}

感谢@MKer 指引我正确的方向。