]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/gui/Viewport.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / gui / Viewport.java
1 package tim.prune.gui;
2
3 import tim.prune.gui.map.MapCanvas;
4 import tim.prune.gui.map.MapPosition;
5 import tim.prune.gui.map.MapUtils;
6
7 /**
8  * Class to provide access to current viewport
9  * The point of this class is to decouple the view from the MapCanvas object
10  * so that when a search function needs to know the area currently viewed, it doesn't
11  * need to have a direct connection to the MapCanvas.  Instead it asks the App for the viewport,
12  * which is then able to get the map position from the MapCanvas.
13  * I'm still not sure whether this is ugly or not, but it's more efficient than constantly listening.
14  */
15 public class Viewport
16 {
17         /** Map canvas object */
18         private MapCanvas _mapCanvas = null;
19
20         /**
21          * Constructor
22          * @param inCanvas map canvas object
23          */
24         public Viewport(MapCanvas inCanvas)
25         {
26                 _mapCanvas = inCanvas;
27         }
28
29         /**
30          * @return coordinate bounds of current viewport
31          */
32         public double[] getBounds()
33         {
34                 int width = _mapCanvas.getWidth();
35                 int height = _mapCanvas.getHeight();
36                 MapPosition mapPosition = _mapCanvas.getMapPosition();
37                 double minLat = MapUtils.getLatitudeFromY(mapPosition.getYFromPixels(height, height));
38                 double maxLat = MapUtils.getLatitudeFromY(mapPosition.getYFromPixels(0, height));
39                 double minLon = MapUtils.getLongitudeFromX(mapPosition.getXFromPixels(0, width));
40                 double maxLon = MapUtils.getLongitudeFromX(mapPosition.getXFromPixels(width, width));
41                 return new double[] {minLat, minLon, maxLat, maxLon};
42         }
43 }