1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/*
* Copyright © 2010 Anthony Towns <aj@erisian.com.au>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package org.altusmetrum.altosuilib_6;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import java.awt.RenderingHints.*;
import org.altusmetrum.altoslib_6.*;
public class AltosUIMapTile {
AltosUIMapTileListener listener;
AltosUILatLon upper_left, center;
int px_size;
int zoom;
int maptype;
AltosUIMapStore store;
AltosUIMapCache cache;
int status;
private File map_file() {
double lat = center.lat;
double lon = center.lon;
char chlat = lat < 0 ? 'S' : 'N';
char chlon = lon < 0 ? 'W' : 'E';
if (lat < 0) lat = -lat;
if (lon < 0) lon = -lon;
String maptype_string = String.format("%s-", AltosUIMap.maptype_names[maptype]);
String format_string;
if (maptype == AltosUIMap.maptype_hybrid || maptype == AltosUIMap.maptype_satellite || maptype == AltosUIMap.maptype_terrain)
format_string = "jpg";
else
format_string = "png";
return new File(AltosUIPreferences.mapdir(),
String.format("map-%c%.6f,%c%.6f-%s%d.%s",
chlat, lat, chlon, lon, maptype_string, zoom, format_string));
}
private String map_url() {
String format_string;
if (maptype == AltosUIMap.maptype_hybrid || maptype == AltosUIMap.maptype_satellite || maptype == AltosUIMap.maptype_terrain)
format_string = "jpg";
else
format_string = "png32";
if (AltosUIVersion.has_google_maps_api_key())
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",
center.lat, center.lon, zoom, px_size, px_size, AltosUIMap.maptype_names[maptype], format_string, AltosUIVersion.google_maps_api_key);
else
return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&sensor=false&maptype=%s&format=%s",
center.lat, center.lon, zoom, px_size, px_size, AltosUIMap.maptype_names[maptype], format_string);
}
private Font font = null;
public void set_font(Font font) {
this.font = font;
}
int painting_serial;
int painted_serial;
Image image;
public void paint_graphics(Graphics2D g2d, AltosUIMapTransform t, int serial) {
if (serial < painted_serial)
return;
Point2D.Double point_double = t.screen(upper_left);
Point point = new Point((int) (point_double.x + 0.5),
(int) (point_double.y + 0.5));
painted_serial = serial;
if (!g2d.hitClip(point.x, point.y, px_size, px_size))
return;
if (image != null) {
g2d.drawImage(image, point.x, point.y, null);
image = null;
} else {
g2d.setColor(Color.GRAY);
g2d.fillRect(point.x, point.y, px_size, px_size);
if (t.has_location()) {
String message = null;
switch (status) {
case AltosUIMapCache.loading:
message = "Loading...";
break;
case AltosUIMapCache.bad_request:
message = "Internal error";
break;
case AltosUIMapCache.failed:
message = "Network error, check connection";
break;
case AltosUIMapCache.forbidden:
message = "Too many requests, try later";
break;
}
if (message != null && font != null) {
g2d.setFont(font);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Rectangle2D bounds = font.getStringBounds(message, g2d.getFontRenderContext());
float x = px_size / 2.0f;
float y = px_size / 2.0f;
x = x - (float) bounds.getWidth() / 2.0f;
y = y + (float) bounds.getHeight() / 2.0f;
g2d.setColor(Color.BLACK);
g2d.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
}
}
}
}
public void set_status(int status) {
this.status = status;
listener.notify_tile(this, status);
}
public void notify_image(Image image) {
listener.notify_tile(this, status);
}
public void paint(Graphics g, AltosUIMapTransform t) {
Graphics2D g2d = (Graphics2D) g;
boolean queued = false;
Point2D.Double point = t.screen(upper_left);
if (!g.hitClip((int) (point.x + 0.5), (int) (point.y + 0.5), px_size, px_size))
return;
++painting_serial;
if (image == null && t.has_location())
image = cache.get(this, store, px_size, px_size);
paint_graphics(g2d, t, painting_serial);
}
public int store_status() {
return store.status();
}
public void add_store_listener(AltosUIMapStoreListener listener) {
store.add_listener(listener);
}
public void remove_store_listener(AltosUIMapStoreListener listener) {
store.remove_listener(listener);
}
public AltosUIMapTile(AltosUIMapTileListener listener, AltosUILatLon upper_left, AltosUILatLon center, int zoom, int maptype, int px_size, Font font) {
this.listener = listener;
this.upper_left = upper_left;
cache = listener.cache();
while (center.lon < -180.0)
center.lon += 360.0;
while (center.lon > 180.0)
center.lon -= 360.0;
this.center = center;
this.zoom = zoom;
this.maptype = maptype;
this.px_size = px_size;
this.font = font;
status = AltosUIMapCache.loading;
store = AltosUIMapStore.get(map_url(), map_file());
}
}
|