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_6;
23 public class AltosMapTile implements AltosFontListener {
24 AltosMapTileListener listener;
25 AltosLatLon upper_left, center;
33 static public final int success = 0;
34 static public final int loading = 1;
35 static public final int failed = 2;
36 static public final int bad_request = 3;
37 static public final int forbidden = 4;
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-", AltosMap.maptype_names[maptype]);
49 if (maptype == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
50 format_string = "jpg";
52 format_string = "png";
53 return new File(AltosPreferences.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 == AltosMap.maptype_hybrid || maptype == AltosMap.maptype_satellite || maptype == AltosMap.maptype_terrain)
61 format_string = "jpg";
63 format_string = "png32";
65 if (AltosVersion.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, AltosMap.maptype_names[maptype], format_string, AltosVersion.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, AltosMap.maptype_names[maptype], format_string);
73 public void font_size_changed(int font_size) {
76 public void set_status(int status) {
78 listener.notify_tile(this, status);
81 public void notify_image(AltosImage image) {
82 listener.notify_tile(this, status);
85 public int store_status() {
86 return store.status();
89 public void add_store_listener(AltosMapStoreListener listener) {
90 store.add_listener(listener);
93 public void remove_store_listener(AltosMapStoreListener listener) {
94 store.remove_listener(listener);
97 public AltosMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
98 this.listener = listener;
99 this.upper_left = upper_left;
101 while (center.lon < -180.0)
103 while (center.lon > 180.0)
106 this.center = center;
108 this.maptype = maptype;
109 this.px_size = px_size;
111 status = AltosMapTile.loading;
112 store = AltosMapStore.get(map_url(), map_file());