Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

java - How to add a ToolTip to MapMarker in JMapViewer

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

The custom MapMarker is:

public class MapMarkerUnit extends MapObjectImpl implements MapMarker

and the Paint Method overide is

public void paint(Graphics g, Point position, int radio) {
    String filename = "marker.png";
    //System.out.print(filename);
    BufferedImage x = null;
    try {
        x = ImageIO.read(getClass().getResource(filename));
    } catch (IOException ex) {
        Logger.getLogger(MapMarkerUnit.class.getName()).log(Level.SEVERE, null, ex);
    }

    g.drawImage(x, position.x-16, position.y-37,null);

    //if(getLayer()==null||getLayer().isVisibleTexts()) paintText(g, new       Point(position.x+20,position.y));
}

Thanks for any help your able to offer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...