- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在向 Github 中的一些示例代码添加功能通过创建 mouseClicked
在 JXMapViewers 上每个航路点的事件。最终,每个航路点都会显示独特的信息,例如坐标。
在我当前的实现中,仅将最后一个元素添加到 Set<MyWaypoint>
向控制台显示以下内容。
mapViewer mouse click has been detected within 10 pixels of a waypoint
对于检测集合中其他路点的 mouseClicked 事件有什么想法吗?
public class Sample4 {
private static JXMapViewer mapViewer;
/**
* @param args the program args (ignored)
*/
public static void main(String[] args) {
// Create a TileFactoryInfo for Virtual Earth
TileFactoryInfo info = new VirtualEarthTileFactoryInfo(VirtualEarthTileFactoryInfo.MAP);
DefaultTileFactory tileFactory = new DefaultTileFactory(info);
tileFactory.setThreadPoolSize(8);
// Setup local file cache
File cacheDir = new File(System.getProperty("user.home") + File.separator + ".jxmapviewer2");
LocalResponseCache.installResponseCache(info.getBaseURL(), cacheDir, false);
// Setup JXMapViewer
mapViewer = new JXMapViewer();
mapViewer.setTileFactory(tileFactory);
GeoPosition frankfurt = new GeoPosition(50, 7, 0, 8, 41, 0);
GeoPosition wiesbaden = new GeoPosition(50, 5, 0, 8, 14, 0);
GeoPosition mainz = new GeoPosition(50, 0, 0, 8, 16, 0);
GeoPosition darmstadt = new GeoPosition(49, 52, 0, 8, 39, 0);
GeoPosition offenbach = new GeoPosition(50, 6, 0, 8, 46, 0);
// Set the focus
mapViewer.setZoom(10);
mapViewer.setAddressLocation(frankfurt);
// Add interactions
MouseInputListener mia = new PanMouseInputListener(mapViewer);
mapViewer.addMouseListener(mia);
mapViewer.addMouseMotionListener(mia);
mapViewer.addMouseListener(new CenterMapListener(mapViewer));
mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCenter(mapViewer));
mapViewer.addKeyListener(new PanKeyListener(mapViewer));
// Create a track from the geo-positions
final List<GeoPosition> track = Arrays.asList(frankfurt, wiesbaden, mainz, darmstadt, offenbach);
RoutePainter routePainter = new RoutePainter(track);
// Create waypoints from the geo-positions
Set<MyWaypoint> waypoints = new HashSet<MyWaypoint>(Arrays.asList(
new MyWaypoint("1", Color.ORANGE, frankfurt),
new MyWaypoint("2", Color.CYAN, wiesbaden),
new MyWaypoint("3", Color.GRAY, mainz),
new MyWaypoint("4", Color.MAGENTA, darmstadt),
new MyWaypoint("5", Color.GREEN, offenbach)));
// Create a waypoint painter that takes all the waypoints
WaypointPainter<MyWaypoint> waypointPainter = new WaypointPainter<MyWaypoint>();
waypointPainter.setWaypoints(waypoints);
waypointPainter.setRenderer(new FancyWaypointRenderer());
// Create a compound painter that uses both the route-painter and the waypoint-painter
List<Painter<JXMapViewer>> painters = new ArrayList<Painter<JXMapViewer>>();
painters.add(routePainter);
painters.add(waypointPainter);
CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
mapViewer.setOverlayPainter(painter);
mapViewer.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
Point2D gp_pt = null;
for (GeoPosition waypoint : track) {
//convert to world bitmap
gp_pt = mapViewer.getTileFactory().geoToPixel(waypoint, mapViewer.getZoom());
}
//convert to screen
Rectangle rect = mapViewer.getViewportBounds();
Point converted_gp_pt = new Point((int) gp_pt.getX() - rect.x,
(int) gp_pt.getY() - rect.y);
//check if near the mouse
if (converted_gp_pt.distance(me.getPoint()) < 10) {
System.out.println("mapViewer mouse click has been detected within 10 pixels of a waypoint");
} else {
System.out.println("mapViewer mouse click has been dected but NOT with 10 pixels of a waypoint ");
}
}
}); // end MouseAdapter
// Display the viewer in a JFrame
JFrame frame = new JFrame("JXMapviewer2 Example 4");
frame.getContentPane().add(mapViewer);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static JXMapViewer getMapViewer() {
return mapViewer;
}
}
最佳答案
我认为,如果你想检查所有航点,你应该在 for 中添加“//转换到屏幕” block 和“//检查是否靠近鼠标”,即:
public void mouseClicked(MouseEvent me) {
Point2D gp_pt = null;
for (GeoPosition waypoint : track) {
//convert to world bitmap
gp_pt = mapViewer.getTileFactory().geoToPixel(waypoint, mapViewer.getZoom());
//convert to screen
Rectangle rect = mapViewer.getViewportBounds();
Point converted_gp_pt = new Point((int) gp_pt.getX() - rect.x,
(int) gp_pt.getY() - rect.y);
//check if near the mouse
if (converted_gp_pt.distance(me.getPoint()) < 10) {
System.out.println("mapViewer mouse click has been detected within 10 pixels of a waypoint");
} else {
System.out.println("mapViewer mouse click has been dected but NOT with 10 pixels of a waypoint ");
}
}
}
关于java - 将 mouseClicked 事件监听器添加到 JXMapViewer 上的航点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406947/
我正在开发一个应用程序并正在尝试 http://today.java.net/pub/a/today/2007/11/13/mapping-mashups-with-jxmapviewer.html
我想编写一个必须使用缩放/平移工具渲染我的 map 图像的小程序,我发现 JXMapViewer 可以帮助我,我该如何使用它? 我在哪里可以找到相关文档,帮助我编写一个小程序,请求在我的图像服务器上提
本文整理了Java中org.jxmapviewer.input.ZoomMouseWheelListenerCursor类的一些代码示例,展示了ZoomMouseWheelListenerCursor
我正在尝试将 JXMapViewer(来自 swingx-ws)与 Open Street Maps 一起使用。我想知道是否可以在 JXMapViewer 中基于航向而不是北方向上显示 map 图 b
本文整理了Java中org.jxmapviewer.input.ZoomMouseWheelListenerCursor.()方法的一些代码示例,展示了ZoomMouseWheelListenerCu
我正在向 Github 中的一些示例代码添加功能通过创建 mouseClicked 在 JXMapViewers 上每个航路点的事件。最终,每个航路点都会显示独特的信息,例如坐标。 在我当前的实现中,
我尝试将 JxMapViewer 集成到我的 swing 应用程序中。 我在 map 上的两个路径点之间画了一条线。现在我希望实现这样的功能: 无论我在 map 上的哪个位置,无论设置了多少缩放级别,
我是一名优秀的程序员,十分优秀!