在地图中的图块上实现鼠标单击事件 [英] Implementing a mouse click event on a tile in a map

查看:158
本文介绍了在地图中的图块上实现鼠标单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JPanel上为图像(基本上是地图上的图块)实现鼠标单击事件。我只是无法弄清楚如何去做。我有一个扩展JPanel的Main类。我正在从tile服务器中检索tile,并根据特定的缩放级别在Main类的paintComponent()方法中显示它们。我使用微小的定位器图像来表示同一个paintComponent()方法中的特定纪念碑或城市中的建筑物。它们根据相应的纬度和经度放置在这些瓷砖的顶部。

I'm trying to implement a mouse click event for an image (basically a tile on a map) on a JPanel. I'm just not able to figure out how to go about it. I have a Main class that extends JPanel. I'm retrieving tiles from a tile server and displaying them in the paintComponent() method of the Main class based on the specific zoom level. I use tiny locator images to represent a specific monument or a building in a city in the same paintComponent() method. They are placed on top of these tiles based on corresponding latitude and longitude.

当我点击这些定位器图像时,我必须能够将事件MouseClick()添加到定位器图像。现在我到目前为止所读到的是我们无法为图像添加事件处理程序。它只能添加到swing的原生组件中。当我没有使用JLabel表示事件时,如何将事件添加到微小的定位器图像中,或者甚至用图像说明用矩形表示?

When I click on these locator images, I must be able to add an event MouseClick() to the locator image. Now what I've read so far is we cannot add an event handler to images. It can only be added to native components of swing. How do I go about adding the events to the tiny locator images when I have not represented it using JLabel or even surrounding the image say with a rectangle?

推荐答案

在具有合适投影的足够小的地图上,您可以使用线性在坐标系之间进行转换插值相对于封闭面板。注意以下比例,您可以交叉乘法并求解缺失的坐标,如映射鼠标的完整示例所示坐标到图像中的像素坐标。

On a sufficiently small map with a suitable projection, you can transform between coordinate systems using linear interpolation relative to the enclosing panel. Noting the following proportions, you can cross-multiply and solve for the missing coordinate, as shown in this complete example that maps mouse coordinates to pixel coordinates in an image.

mouse.x : panelWidthInPixels :: featureLongitude : tileWidthInDegrees
mouse.y : panelHeightInPixels :: featureLatitude : tileHeightInDegrees

更一般地说,使用像< a href =https://stackoverflow.com/tags/jmapviewer/info> JMapViewer ,它将地图的投影合并到转换中。 此处列出了命中测试功能的架构。

More generally, use a library like JMapViewer that incorporates the map's projection into the transformation. A schema for hit-testing features is outlined here.

这篇关于在地图中的图块上实现鼠标单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆