How to add a ToolTip to MapMarker in JMapViewer

前端 未结 1 2007
感动是毒
感动是毒 2020-12-21 13:58

I\'m trying to add a ToolTip to a custom MapMarker on JMapViewer. But repeaded searches on are not helping me solve this.

The cust

相关标签:
1条回答
  • 2020-12-21 14:49

    Override the getToolTipText() method of JMapViewer. In your implementation, use getPosition() to convert the MouseEvent coordinates into geodetic coordinates. The example below simply displays the unformatted coordinates; you'll want to find the nearest MapMarker and return the appropriate text.

    JMapViewer map = new JMapViewer() {
    
        @Override
        public String getToolTipText(MouseEvent e) {
            Coordinate c = getPosition(e.getX(), e.getY());
            return c.getLat() + " " + c.getLon();
        }
    };
    map.setToolTipText(""); // initialize 
    

    Addendum: Is there a way of adding a tooltip directly to an image?

    No; JMapViewer is the enclosing JComponent that handles tool tips.

    I have about 50 markers on the map…that's a lot of iterations.

    You definitely can't load images in your MapMarker implementation; use a SWingWorker to load images in the background, for example.

    As a concrete iteration example, JFreeChart easily handles tool tips for scores of entities in this way. Here's the enclosing panel's getToolTipText() implementation, and here's the loop that invokes Shape#contains(). A simplified example that illustrates the approach is seen here.

    0 讨论(0)
提交回复
热议问题