Implementing a mouse click event on a tile in a map

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.

Leave a Comment