Jxmaps How can I add a ContextMenu to a Marker?

135 views Asked by At

I've been trying to add a contextmenu to a marker, but I can't figure out how to get the contextmenu to appear.

marker.addEventListener("rightclick", new MapMouseEvent() {
    @Override
    public void onEvent(MouseEvent event) {
        contextMenu.show(
            marker, marker.getPosition().getLat(), 
            marker.getPosition().getLng()
        );
    }
});

I tried to cast marker as Node, but that didn't work, help?

1

There are 1 answers

3
Serhii Fedchenko On

To display a popup menu on JxMaps you have to do the next actions:

  1. Switch JxMaps to the LIGHTWEIGHT mode (). In the HEAVYWEIGHT mode, the popup menu can be displayed under the map.
  2. Add the code that shows the PopupMenu to "click" the event handler.

Please take a look at the following example:

JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Test"));

MapView mapView = new MapView(new MapViewOptions(MapComponentType.LIGHTWEIGHT));
mapView.setOnMapReadyHandler(new MapReadyHandler() {
    @Override
    public void onMapReady(MapStatus status) {
        final Map map = mapView.getMap();
        map.setCenter(new LatLng(35.91466, 10.312499));
        map.setZoom(2.0);
        map.addEventListener("rightclick", new MapEvent() {
            @Override
            public void onEvent() {
                java.awt.Point pos = MouseInfo.getPointerInfo().getLocation();
                SwingUtilities.convertPointFromScreen(pos, mapView);
                popup.show(mapView, pos.x, pos.y);
            }
        });
    }
});