1 package tim.prune.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 public 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;
30 // TODO: Add other map sources, eg Yahoo, MSN, search.ch ?
33 * Generate a URL for the given source and track info
34 * @param inSource source to use, either google or openstreetmap
35 * @param inTrackInfo track info
38 public static String generateUrl(int inSource, TrackInfo inTrackInfo)
40 if (inSource == MAP_SOURCE_GOOGLE) {
41 return generateGoogleUrl(inTrackInfo);
43 return generateOpenStreetMapUrl(inTrackInfo);
47 * Generate a url for Google maps
48 * @param inTrackInfo track information
51 private static String generateGoogleUrl(TrackInfo inTrackInfo)
53 // Check if any data to display
54 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
58 double medianLat = getMedianValue(inTrackInfo.getTrack().getLatRange());
59 double medianLon = getMedianValue(inTrackInfo.getTrack().getLonRange());
60 double latSpan = getSpan(inTrackInfo.getTrack().getLatRange());
61 double lonSpan = getSpan(inTrackInfo.getTrack().getLonRange());
62 // Build basic url with centre position and span
63 String url = "http://" + I18nManager.getText("url.googlemaps")
64 + "/?ll=" + FIVE_DP.format(medianLat) + "," + FIVE_DP.format(medianLon)
65 + "&spn=" + FIVE_DP.format(latSpan) + "," + FIVE_DP.format(lonSpan);
66 DataPoint currPoint = inTrackInfo.getCurrentPoint();
67 // Add selected point, if any
68 if (currPoint != null) {
69 url = url + "&q=" + FIVE_DP.format(currPoint.getLatitude().getDouble()) + ","
70 + FIVE_DP.format(currPoint.getLongitude().getDouble());
71 if (currPoint.getWaypointName() != null) {
72 url = url + "(" + currPoint.getWaypointName() + ")";
75 //System.out.println(url);
80 * Generate a url for Open Street Map
81 * @param inTrackInfo track information
84 private static String generateOpenStreetMapUrl(TrackInfo inTrackInfo)
86 // Check if any data to display
87 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
91 DoubleRange latRange = inTrackInfo.getTrack().getLatRange();
92 DoubleRange lonRange = inTrackInfo.getTrack().getLonRange();
93 // Build basic url using min and max lat and long
94 String url = "http://openstreetmap.org/?minlat=" + FIVE_DP.format(latRange.getMinimum())
95 + "&maxlat=" + FIVE_DP.format(latRange.getMaximum())
96 + "&minlon=" + FIVE_DP.format(lonRange.getMinimum()) + "&maxlon=" + FIVE_DP.format(lonRange.getMaximum());
97 DataPoint currPoint = inTrackInfo.getCurrentPoint();
98 // Add selected point, if any (no way to add point name?)
99 if (currPoint != null) {
100 url = url + "&mlat=" + FIVE_DP.format(currPoint.getLatitude().getDouble())
101 + "&mlon=" + FIVE_DP.format(currPoint.getLongitude().getDouble());
107 * Get the median value from the given lat/long range
108 * @param inRange range of values
109 * @return median value
111 private static double getMedianValue(DoubleRange inRange)
113 return (inRange.getMaximum() + inRange.getMinimum()) / 2.0;
117 * Get the span of the given lat/long range
118 * @param inRange range of values
121 private static double getSpan(DoubleRange inRange)
123 return inRange.getMaximum() - inRange.getMinimum();