2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 import java.awt.image.*;
22 import java.awt.event.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
29 import java.util.prefs.*;
31 import java.net.URLConnection;
33 public class AltosSiteMapLabel extends JLabel {
34 public static boolean fetchMap(File file, String url) {
38 } catch (java.net.MalformedURLException e) {
44 URLConnection uc = u.openConnection();
45 int contentLength = uc.getContentLength();
46 InputStream in = new BufferedInputStream(uc.getInputStream());
49 data = new byte[contentLength];
50 while (offset < contentLength) {
51 bytesRead = in.read(data, offset, data.length - offset);
58 if (offset != contentLength) {
61 } catch (IOException e) {
66 FileOutputStream out = new FileOutputStream(file);
70 } catch (FileNotFoundException e) {
72 } catch (IOException e) {
81 public void fetchAndLoadMap(final File pngfile, final String url) {
82 System.out.printf("# Trying to fetch %s...\n", pngfile);
84 Thread thread = new Thread() {
87 if (fetchMap(pngfile, url)) {
88 setIcon(new ImageIcon(ImageIO.read(pngfile)));
90 } catch (Exception e) {
91 System.out.printf("# Failed to fetch file %s\n", pngfile);
92 System.out.printf(" wget -O '%s' ''\n", pngfile, url);
99 public void fetchMap(double lat, double lng, int zoom, int px_size) {
100 File pngfile = MapFile(lat, lng, zoom);
101 String url = MapURL(lat, lng, zoom, px_size);
103 if (!pngfile.exists()) {
104 fetchMap(pngfile, url);
108 public void loadMap(double lat, double lng, int zoom, int px_size) {
109 File pngfile = MapFile(lat, lng, zoom);
110 String url = MapURL(lat, lng, zoom, px_size);
112 if (!pngfile.exists()) {
113 fetchAndLoadMap(pngfile, url);
118 setIcon(new ImageIcon(ImageIO.read(pngfile)));
120 } catch (IOException e) {
121 System.out.printf("# IO error trying to load %s\n", pngfile);
126 private static File MapFile(double lat, double lng, int zoom) {
127 char chlat = lat < 0 ? 'S' : 'N';
128 char chlng = lng < 0 ? 'E' : 'W';
129 if (lat < 0) lat = -lat;
130 if (lng < 0) lng = -lng;
131 return new File(AltosPreferences.logdir(),
132 String.format("map-%c%.6f,%c%.6f-%d.png",
133 chlat, lat, chlng, lng, zoom));
136 private static String MapURL(double lat, double lng,
137 int zoom, int px_size)
139 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);