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.
Override the
getToolTipText()
method ofJMapViewer
. In your implementation, usegetPosition()
to convert theMouseEvent
coordinates into geodetic coordinates. The example below simply displays the unformatted coordinates; you'll want find the nearestMapMarker
and return the appropriate text.Addendum: Is there a way of adding a tooltip directly to an image?
No;
JMapViewer
is the enclosingJComponent
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 aSWingWorker
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'sgetToolTipText()
implementation, and here's the loop that invokesShape#contains()
. A simplified example that illustrates the approach is seen here.