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.altoslib_8;
23 public abstract class AltosMapTile implements AltosFontListener {
24 AltosMapTileListener listener;
25 public AltosLatLon upper_left, center;
30 public AltosMapStore store;
31 public AltosMapCache cache;
34 static public final int success = 0;
35 static public final int loading = 1;
36 static public final int failed = 2;
37 static public final int bad_request = 3;
38 static public final int forbidden = 4;
40 private File map_file() {
41 double lat = center.lat;
42 double lon = center.lon;
43 char chlat = lat < 0 ? 'S' : 'N';
44 char chlon = lon < 0 ? 'W' : 'E';
46 if (lat < 0) lat = -lat;
47 if (lon < 0) lon = -lon;
48 String maptype_string = String.format("%s-", AltosMap.maptype_names[maptype]);
50 if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
51 format_string = "jpg";
53 format_string = "png";
54 return new File(AltosPreferences.mapdir(),
55 String.format("map-%c%.6f,%c%.6f-%s%d%s.%s",
56 chlat, lat, chlon, lon, maptype_string, zoom, scale == 1 ? "" : String.format("-%d", scale), format_string));
59 private String map_url() {
63 if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
64 format_string = "jpg";
66 format_string = "png32";
68 for (int s = 1; s < scale; s <<= 1)
71 if (AltosVersion.has_google_maps_api_key())
72 return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&scale=%d&sensor=false&maptype=%s&format=%s&key=%s",
73 center.lat, center.lon, z, px_size/scale, px_size/scale, scale, AltosMap.maptype_names[maptype], format_string, AltosVersion.google_maps_api_key);
75 return String.format("http://maps.google.com/maps/api/staticmap?center=%.6f,%.6f&zoom=%d&size=%dx%d&scale=%d&sensor=false&maptype=%s&format=%s",
76 center.lat, center.lon, z, px_size/scale, px_size/scale, AltosMap.maptype_names[maptype], format_string);
79 public void font_size_changed(int font_size) {
82 public void set_status(int status) {
84 listener.notify_tile(this, status);
87 public void notify_image(AltosImage image) {
88 listener.notify_tile(this, status);
91 public int store_status() {
92 return store.status();
95 public void add_store_listener(AltosMapStoreListener listener) {
96 store.add_listener(listener);
99 public void remove_store_listener(AltosMapStoreListener listener) {
100 store.remove_listener(listener);
103 public abstract void paint(AltosMapTransform t);
105 public AltosMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
106 this.listener = listener;
107 this.upper_left = upper_left;
108 this.cache = listener.cache();
110 while (center.lon < -180.0)
112 while (center.lon > 180.0)
115 this.center = center;
117 this.maptype = maptype;
118 this.px_size = px_size;
121 status = AltosMapTile.loading;
122 store = AltosMapStore.get(map_url(), map_file());
125 public AltosMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
126 this(listener, upper_left, center, zoom, maptype, px_size, 1);