X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosui%2FAltosSiteMap.java;h=4195fc3ff186858bfb17834b109fdd237a67b0d3;hp=7306813806e0e6034719c7d202dd5748d3005892;hb=708e7937cba52982b91244cf89bfbff46d346135;hpb=abb8510b97ce9cbbff0275cc31f74780fe1ce138 diff --git a/altosui/AltosSiteMap.java b/altosui/AltosSiteMap.java index 73068138..4195fc3f 100644 --- a/altosui/AltosSiteMap.java +++ b/altosui/AltosSiteMap.java @@ -15,7 +15,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package altosui; +package AltosUI; import java.awt.*; import java.awt.image.*; @@ -31,6 +31,8 @@ import java.util.prefs.*; import java.lang.Math; import java.awt.geom.Point2D; import java.awt.geom.Line2D; +import java.util.concurrent.*; +import org.altusmetrum.AltosLib.*; public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { // preferred vertical step in a tile in naut. miles @@ -97,6 +99,8 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { int zoom; double scale_x, scale_y; + int radius; /* half width/height of tiles to load */ + private Point2D.Double pt(double lat, double lng) { return pt(new LatLng(lat, lng), scale_x, scale_y); } @@ -108,7 +112,7 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { return latlng(pt, scale_x, scale_y); } - HashMap mapTiles = new HashMap(); + ConcurrentHashMap mapTiles = new ConcurrentHashMap(); Point2D.Double centre; private Point2D.Double tileCoordOffset(Point p) { @@ -144,37 +148,35 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { // nothing } - private void bgLoadMap(final AltosSiteMapTile tile, - final File pngfile, final String pngurl) + public void set_font() { + // nothing + } + + private void loadMap(final AltosSiteMapTile tile, + File pngfile, String pngurl) { - //System.out.printf("Loading/fetching map %s\n", pngfile); - Thread thread = new Thread() { - public void run() { - final ImageIcon res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl); - if (res != null) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - tile.loadMap(res); - } - }); - } else { - System.out.printf("# Failed to fetch file %s\n", pngfile); - System.out.printf(" wget -O '%s' ''\n", pngfile, pngurl); - } - } - }; - thread.start(); + final ImageIcon res = AltosSiteMapCache.fetchAndLoadMap(pngfile, pngurl); + if (res != null) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + tile.loadMap(res); + } + }); + } else { + System.out.printf("# Failed to fetch file %s\n", pngfile); + System.out.printf(" wget -O '%s' '%s'\n", pngfile, pngurl); + } } - File pngfile; - String pngurl; + File pngfile; + String pngurl; public int prefetchMap(int x, int y) { LatLng map_latlng = latlng( -centre.x + x*px_size + px_size/2, -centre.y + y*px_size + px_size/2); - pngfile = MapFile(map_latlng.lat, map_latlng.lng); - pngurl = MapURL(map_latlng.lat, map_latlng.lng); + pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom); + pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom); if (pngfile.exists()) { return 1; } else if (AltosSiteMapCache.fetchMap(pngfile, pngurl)) { @@ -210,35 +212,51 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { } } - private void initMap(AltosSiteMapTile tile, Point offset) { + public String initMap(Point offset) { + AltosSiteMapTile tile = mapTiles.get(offset); Point2D.Double coord = tileCoordOffset(offset); LatLng map_latlng = latlng(px_size/2-coord.x, px_size/2-coord.y); - File pngfile = MapFile(map_latlng.lat, map_latlng.lng); - String pngurl = MapURL(map_latlng.lat, map_latlng.lng); - bgLoadMap(tile, pngfile, pngurl); + File pngfile = MapFile(map_latlng.lat, map_latlng.lng, zoom); + String pngurl = MapURL(map_latlng.lat, map_latlng.lng, zoom); + loadMap(tile, pngfile, pngurl); + return pngfile.toString(); } - private void initMaps(double lat, double lng) { - centre = getBaseLocation(lat, lng); - + public void setBaseLocation(double lat, double lng) { for (Point k : mapTiles.keySet()) { - initMap(mapTiles.get(k), k); + AltosSiteMapTile tile = mapTiles.get(k); + tile.clearMap(); } + + centre = getBaseLocation(lat, lng); + scrollRocketToVisible(pt(lat,lng)); } - private File MapFile(double lat, double lng) { + private void initMaps(double lat, double lng) { + setBaseLocation(lat, lng); + + Thread thread = new Thread() { + public void run() { + for (Point k : mapTiles.keySet()) + initMap(k); + } + }; + thread.start(); + } + + private static File MapFile(double lat, double lng, int zoom) { char chlat = lat < 0 ? 'S' : 'N'; char chlng = lng < 0 ? 'W' : 'E'; if (lat < 0) lat = -lat; if (lng < 0) lng = -lng; - return new File(AltosPreferences.mapdir(), + return new File(AltosUIPreferences.mapdir(), String.format("map-%c%.6f,%c%.6f-%d.png", chlat, lat, chlng, lng, zoom)); } - private String MapURL(double lat, double lng) { + private static String MapURL(double lat, double lng, int zoom) { return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=hybrid&format=png32", lat, lng, zoom, px_size, px_size); } @@ -248,7 +266,6 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { public void show(double lat, double lon) { initMaps(lat, lon); - initialised = true; scrollRocketToVisible(pt(lat, lon)); } public void show(final AltosState state, final int crc_errors) { @@ -295,7 +312,7 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { AltosSiteMapTile tile = createTile(offset); tile.show(state, crc_errors, lref, ref); - initMap(tile, offset); + initMap(offset); finishTileLater(tile, offset); } @@ -309,6 +326,16 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { last_state = state.state; } + public void draw_circle(double lat, double lon) { + final Point2D.Double pt = pt(lat, lon); + + for (Point offset : mapTiles.keySet()) { + AltosSiteMapTile tile = mapTiles.get(offset); + Point2D.Double ref = translatePoint(pt, tileCoordOffset(offset)); + tile.draw_circle(ref); + } + } + private AltosSiteMapTile createTile(Point offset) { AltosSiteMapTile tile = new AltosSiteMapTile(px_size); mapTiles.put(offset, tile); @@ -325,13 +352,13 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { } private void ensureTilesAround(Point base_offset) { - for (int x = -1; x <= 1; x++) { - for (int y = -1; y <= 1; y++) { + for (int x = -radius; x <= radius; x++) { + for (int y = -radius; y <= radius; y++) { Point offset = new Point(base_offset.x + x, base_offset.y + y); if (mapTiles.containsKey(offset)) continue; AltosSiteMapTile tile = createTile(offset); - initMap(tile, offset); + initMap(offset); finishTileLater(tile, offset); } } @@ -396,13 +423,15 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { JComponent comp = new JComponent() { }; private GridBagLayout layout = new GridBagLayout(); - public AltosSiteMap() { + public AltosSiteMap(int in_radius) { + radius = in_radius; + GrabNDrag scroller = new GrabNDrag(comp); comp.setLayout(layout); - for (int x = -1; x <= 1; x++) { - for (int y = -1; y <= 1; y++) { + for (int x = -radius; x <= radius; x++) { + for (int y = -radius; y <= radius; y++) { Point offset = new Point(x, y); AltosSiteMapTile t = createTile(offset); addTileAt(t, offset); @@ -411,4 +440,8 @@ public class AltosSiteMap extends JScrollPane implements AltosFlightDisplay { setViewportView(comp); setPreferredSize(new Dimension(500,500)); } + + public AltosSiteMap() { + this(1); + } }