Search code examples
androidcontextmenuosmdroid

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


My goal: i want to show a popupmenu next to a osmdroid-marker

My problem:

  • In Android you can attach a android.widget.PopupMenu to a android.view.View : the menu is shown next to the view.
  • In osmdroid a marker is inherited from org.osmdroid.views.overlay.Overlay. Unfortunately Overlay is not inherited from View so i cannot attach a popup to it.

Currently i am attaching the menu to an image that is in the lower right corner but may be far away from the current marker.

This is my current code (from 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;
    }
}

My question: is there a simple way to create a temporary 1x1 pixel window on top of my marker at IGeoPoint geoPosition where i can attach the popup to?

Or is there any other simple way to position the menu?


Solution

  • Use the OSMBonusPack Marker InfoWindow (Marker bubble).

    You can get its view with infowindow.getView(), and this is a regular android.view.View.

    You can design its layout to make it as small as needed.