Osmdroid:如何在Marker旁边创建一个Contextmenu?

时间:2015-09-30 12:07:53

标签: android contextmenu osmdroid

我的目标:我想在osmdroid-marker

旁边显示一个弹出菜单

我的问题:

  • 在Android中,您可以将android.widget.PopupMenu附加到android.view.View:视图旁边会显示菜单。
  • 在osmdroid中,标记继承自org.osmdroid.views.overlay.Overlay。不幸的是,Overlay并不是从View继承的,因此我无法在其中附加弹出窗口。

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

这是我目前的代码(来自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像素窗口,我可以将弹出窗口附加到?

还是有其他简单的方法来定位菜单吗?

3 个答案:

答案 0 :(得分:1)

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

您可以使用infowindow.getView()获取其视图,这是一个常规的android.view.View。

您可以设计其布局,使其尽可能小。

答案 1 :(得分:0)

与MKer一样,您可以使用OSMBonusPack Marker InfoWindow并自定义您自己的InfoWindow。 https://mobiledevstories.wordpress.com/2014/03/01/osmdroid-bonus-pack-markers-with-clickable-infowindows/

答案 2 :(得分:0)

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

/** 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指导我走向正确的方向。