Add mapView to JPanel in java

915 views Asked by At

I want to add a map view in a JPanel, i am using jxMaps from teamdev. I try this code, but i doesn't work.

    Map sample = new Map();

    JFrame frame = new JFrame("Map");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLayout(null);
    frame.setSize(700, 500);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    JPanel panelMap = new JPanel();
    panelMap.setLayout(null);
    panelMap.add(sample, BorderLayout.CENTER);
    panelMap.setBounds(100, 100, 200, 200);
    panelMap.setVisible(true);
    frame.add(panelMap);

I hope you can help me!

Here the class Map

    public class Map extends MapView {
    public Map() {

    setOnMapReadyHandler(new MapReadyHandler() {
        @SuppressWarnings("deprecation")
        @Override
        public void onMapReady(MapStatus status) {
            if (status == MapStatus.MAP_STATUS_OK) {
                final Map map = getMap();
                MapOptions options = new MapOptions(map);
                MapTypeControlOptions controlOptions = new MapTypeControlOptions(map);
                controlOptions.setPosition(ControlPosition.TOP_RIGHT);
                options.setMapTypeControlOptions(controlOptions);
                map.setOptions(options);
                map.setCenter(new LatLng(map, 35.91466, 10.312499));

                map.setZoom(2.0);

            }
        }
    });
}

This example is from jxMap ,https://www.teamdev.com/jxmaps

1

There are 1 answers

0
Vitaly Eremenko On BEST ANSWER

It seems that there is layout error. If you set null layout for container you have to set bounds for each child component manually. Your code sample didn't have any calls that set bounds for Map instance. In other words, after:

panelMap.setLayout(null);

you have to set bounds for Map instance:

sample.setBounds(0, 0, 100, 100);