Implementing a mouse click event on a tile in a map

喜你入骨 提交于 2019-11-26 14:55:20

问题


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.

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/32170318/implementing-a-mouse-click-event-on-a-tile-in-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!