1 package tim.prune.function.browser;
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.util.Locale;
7 import tim.prune.I18nManager;
8 import tim.prune.data.DataPoint;
9 import tim.prune.data.DoubleRange;
10 import tim.prune.data.TrackInfo;
13 * Class to manage the generation of map urls
14 * for display in an external browser
16 public abstract class UrlGenerator
18 /** Number formatter for five dp */
19 private static final NumberFormat FIVE_DP = NumberFormat.getNumberInstance(Locale.UK);
20 // Select the UK locale for this formatter so that decimal point is always used (not comma)
22 if (FIVE_DP instanceof DecimalFormat) ((DecimalFormat) FIVE_DP).applyPattern("0.00000");
25 /** Constant for Google Maps */
26 public static final int MAP_SOURCE_GOOGLE = 0;
27 /** Constant for Open Street Maps */
28 public static final int MAP_SOURCE_OSM = 1;
29 /** Constant for Mapquest */
30 public static final int MAP_SOURCE_MAPQUEST = 2;
31 /** Constant for Yahoo */
32 public static final int MAP_SOURCE_YAHOO = 3;
34 // TODO: Add other map sources, eg MSN, search.ch ?
37 * Generate a URL for the given source and track info
38 * @param inSource source to use, either google or openstreetmap
39 * @param inTrackInfo track info
42 public static String generateUrl(int inSource, TrackInfo inTrackInfo)
44 if (inSource == MAP_SOURCE_GOOGLE) {
45 return generateGoogleUrl(inTrackInfo);
47 else if (inSource == MAP_SOURCE_MAPQUEST) {
48 return generateMapquestUrl(inTrackInfo);
50 else if (inSource == MAP_SOURCE_YAHOO) {
51 return generateYahooUrl(inTrackInfo);
53 return generateOpenStreetMapUrl(inTrackInfo);
57 * Generate a url for Google maps
58 * @param inTrackInfo track information
61 private static String generateGoogleUrl(TrackInfo inTrackInfo)
63 // Check if any data to display
64 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
68 double medianLat = getMedianValue(inTrackInfo.getTrack().getLatRange());
69 double medianLon = getMedianValue(inTrackInfo.getTrack().getLonRange());
70 double latSpan = getSpan(inTrackInfo.getTrack().getLatRange());
71 double lonSpan = getSpan(inTrackInfo.getTrack().getLonRange());
72 // Build basic url with centre position and span
73 String url = "http://" + I18nManager.getText("url.googlemaps")
74 + "/?ll=" + FIVE_DP.format(medianLat) + "," + FIVE_DP.format(medianLon)
75 + "&spn=" + FIVE_DP.format(latSpan) + "," + FIVE_DP.format(lonSpan);
76 DataPoint currPoint = inTrackInfo.getCurrentPoint();
77 // Add selected point, if any
78 if (currPoint != null) {
79 url = url + "&q=" + FIVE_DP.format(currPoint.getLatitude().getDouble()) + ","
80 + FIVE_DP.format(currPoint.getLongitude().getDouble());
81 if (currPoint.getWaypointName() != null) {
82 url = url + "(" + currPoint.getWaypointName() + ")";
85 //System.out.println(url);
90 * Generate a url for Open Street Map
91 * @param inTrackInfo track information
94 private static String generateOpenStreetMapUrl(TrackInfo inTrackInfo)
96 // Check if any data to display
97 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
101 DoubleRange latRange = inTrackInfo.getTrack().getLatRange();
102 DoubleRange lonRange = inTrackInfo.getTrack().getLonRange();
103 // Build basic url using min and max lat and long
104 String url = "http://openstreetmap.org/?minlat=" + FIVE_DP.format(latRange.getMinimum())
105 + "&maxlat=" + FIVE_DP.format(latRange.getMaximum())
106 + "&minlon=" + FIVE_DP.format(lonRange.getMinimum()) + "&maxlon=" + FIVE_DP.format(lonRange.getMaximum());
107 DataPoint currPoint = inTrackInfo.getCurrentPoint();
108 // Add selected point, if any (no way to add point name?)
109 if (currPoint != null) {
110 url = url + "&mlat=" + FIVE_DP.format(currPoint.getLatitude().getDouble())
111 + "&mlon=" + FIVE_DP.format(currPoint.getLongitude().getDouble());
117 * Generate a url for Mapquest maps
118 * @param inTrackInfo track information
121 private static String generateMapquestUrl(TrackInfo inTrackInfo)
123 // Check if any data to display
124 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
128 double medianLat = getMedianValue(inTrackInfo.getTrack().getLatRange());
129 double medianLon = getMedianValue(inTrackInfo.getTrack().getLonRange());
130 // Build basic url with centre position
131 String url = "http://atlas.mapquest.com/maps/map.adp?latlongtype=decimal&latitude="
132 + FIVE_DP.format(medianLat) + "&longitude=" + FIVE_DP.format(medianLon);
138 * Generate a url for Yahoo maps
139 * @param inTrackInfo track information
142 private static String generateYahooUrl(TrackInfo inTrackInfo)
144 // Check if any data to display
145 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
149 double medianLat = getMedianValue(inTrackInfo.getTrack().getLatRange());
150 double medianLon = getMedianValue(inTrackInfo.getTrack().getLonRange());
151 // Build basic url with centre position
152 String url = "http://maps.yahoo.com/#lat=" + FIVE_DP.format(medianLat)
153 + "&lon=" + FIVE_DP.format(medianLon) + "&zoom=13";
158 * Get the median value from the given lat/long range
159 * @param inRange range of values
160 * @return median value
162 private static double getMedianValue(DoubleRange inRange)
164 return (inRange.getMaximum() + inRange.getMinimum()) / 2.0;
168 * Get the span of the given lat/long range
169 * @param inRange range of values
172 private static double getSpan(DoubleRange inRange)
174 return inRange.getMaximum() - inRange.getMinimum();