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.
18 package org.altusmetrum.altosuilib_2;
21 import java.awt.image.*;
23 import javax.imageio.*;
24 import java.awt.geom.*;
27 import java.awt.RenderingHints.*;
28 import org.altusmetrum.altoslib_4.*;
30 public class AltosUIMapTile {
31 AltosUIMapTileListener listener;
32 AltosUILatLon upper_left, center;
36 AltosUIMapStore store;
39 private File map_file() {
40 double lat = center.lat;
41 double lon = center.lon;
42 char chlat = lat < 0 ? 'S' : 'N';
43 char chlon = lon < 0 ? 'W' : 'E';
45 if (lat < 0) lat = -lat;
46 if (lon < 0) lon = -lon;
47 String maptype_string = String.format("%s-", AltosUIMap.maptype_names[maptype]);
49 if (maptype == AltosUIMap.maptype_hybrid || maptype == AltosUIMap.maptype_satellite || maptype == AltosUIMap.maptype_terrain)
50 format_string = "jpg";
52 format_string = "png";
53 return new File(AltosUIPreferences.mapdir(),
54 String.format("map-%c%.6f,%c%.6f-%s%d.%s",
55 chlat, lat, chlon, lon, maptype_string, zoom, format_string));
58 private String map_url() {
60 if (maptype == AltosUIMap.maptype_hybrid || maptype == AltosUIMap.maptype_satellite || maptype == AltosUIMap.maptype_terrain)
61 format_string = "jpg";
63 format_string = "png32";
65 if (AltosUIVersion.has_google_maps_api_key())
66 return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s&key=%s",
67 center.lat, center.lon, zoom, px_size, px_size, AltosUIMap.maptype_names[maptype], format_string, AltosUIVersion.google_maps_api_key);
69 return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s",
70 center.lat, center.lon, zoom, px_size, px_size, AltosUIMap.maptype_names[maptype], format_string);
72 private Font font = null;
74 public void set_font(Font font) {
83 public void paint_graphics(Graphics2D g2d, AltosUIMapTransform t, int serial) {
84 if (serial < painted_serial)
87 Point2D.Double point_double = t.screen(upper_left);
88 Point point = new Point((int) (point_double.x + 0.5),
89 (int) (point_double.y + 0.5));
91 painted_serial = serial;
93 if (!g2d.hitClip(point.x, point.y, px_size, px_size))
97 g2d.drawImage(image, point.x, point.y, null);
100 g2d.setColor(Color.GRAY);
101 g2d.fillRect(point.x, point.y, px_size, px_size);
103 if (t.has_location()) {
104 String message = null;
106 case AltosUIMapCache.loading:
107 message = "Loading...";
109 case AltosUIMapCache.bad_request:
110 message = "Internal error";
112 case AltosUIMapCache.failed:
113 message = "Network error, check connection";
115 case AltosUIMapCache.forbidden:
116 message = "Too many requests, try later";
119 if (message != null && font != null) {
121 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
122 Rectangle2D bounds = font.getStringBounds(message, g2d.getFontRenderContext());
124 float x = px_size / 2.0f;
125 float y = px_size / 2.0f;
126 x = x - (float) bounds.getWidth() / 2.0f;
127 y = y + (float) bounds.getHeight() / 2.0f;
128 g2d.setColor(Color.BLACK);
129 g2d.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
135 public void set_status(int status) {
136 this.status = status;
137 listener.notify_tile(this, status);
140 public void notify_image(Image image) {
141 listener.notify_tile(this, status);
144 public void paint(Graphics g, AltosUIMapTransform t) {
145 Graphics2D g2d = (Graphics2D) g;
146 boolean queued = false;
148 Point2D.Double point = t.screen(upper_left);
150 if (!g.hitClip((int) (point.x + 0.5), (int) (point.y + 0.5), px_size, px_size))
155 if (image == null && t.has_location())
156 image = AltosUIMapCache.get(this, store, px_size, px_size);
158 paint_graphics(g2d, t, painting_serial);
161 public int store_status() {
162 return store.status();
165 public void add_store_listener(AltosUIMapStoreListener listener) {
166 store.add_listener(listener);
169 public void remove_store_listener(AltosUIMapStoreListener listener) {
170 store.remove_listener(listener);
173 public AltosUIMapTile(AltosUIMapTileListener listener, AltosUILatLon upper_left, AltosUILatLon center, int zoom, int maptype, int px_size, Font font) {
174 this.listener = listener;
175 this.upper_left = upper_left;
177 while (center.lon < -180.0)
179 while (center.lon > 180.0)
182 this.center = center;
184 this.maptype = maptype;
185 this.px_size = px_size;
187 status = AltosUIMapCache.loading;
188 store = AltosUIMapStore.get(map_url(), map_file());