1 package tim.prune.browser;
3 import java.text.DecimalFormat;
5 import tim.prune.I18nManager;
6 import tim.prune.data.DataPoint;
7 import tim.prune.data.DoubleRange;
8 import tim.prune.data.TrackInfo;
11 * Class to manage the generation of map urls
12 * for display in an external browser
14 public abstract class UrlGenerator
16 /** Number formatter for five dp */
17 public static final DecimalFormat FIVE_DP = new DecimalFormat("0.00000");
19 /** Constant for Google Maps */
20 public static final int MAP_SOURCE_GOOGLE = 0;
21 /** Constant for Open Street Maps */
22 public static final int MAP_SOURCE_OSM = 1;
24 // TODO: Add other map sources, eg Yahoo, MSN, search.ch ?
27 * Generate a URL for the given source and track info
28 * @param inSource source to use, either google or openstreetmap
29 * @param inTrackInfo track info
32 public static String generateUrl(int inSource, TrackInfo inTrackInfo)
34 if (inSource == MAP_SOURCE_GOOGLE) {
35 return generateGoogleUrl(inTrackInfo);
37 return generateOpenStreetMapUrl(inTrackInfo);
41 * Generate a url for Google maps
42 * @param inTrackInfo track information
45 private static String generateGoogleUrl(TrackInfo inTrackInfo)
47 // Check if any data to display
48 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
52 double medianLat = getMedianValue(inTrackInfo.getTrack().getLatRange());
53 double medianLon = getMedianValue(inTrackInfo.getTrack().getLonRange());
54 double latSpan = getSpan(inTrackInfo.getTrack().getLatRange());
55 double lonSpan = getSpan(inTrackInfo.getTrack().getLonRange());
56 // Build basic url with centre position and span
57 String url = "http://" + I18nManager.getText("url.googlemaps")
58 + "/?ll=" + FIVE_DP.format(medianLat) + "," + FIVE_DP.format(medianLon)
59 + "&spn=" + FIVE_DP.format(latSpan) + "," + FIVE_DP.format(lonSpan);
60 DataPoint currPoint = inTrackInfo.getCurrentPoint();
61 // Add selected point, if any
62 if (currPoint != null) {
63 url = url + "&q=" + FIVE_DP.format(currPoint.getLatitude().getDouble()) + ","
64 + FIVE_DP.format(currPoint.getLongitude().getDouble());
65 if (currPoint.getWaypointName() != null) {
66 url = url + "(" + currPoint.getWaypointName() + ")";
73 * Generate a url for Open Street Map
74 * @param inTrackInfo track information
77 private static String generateOpenStreetMapUrl(TrackInfo inTrackInfo)
79 // Check if any data to display
80 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
84 DoubleRange latRange = inTrackInfo.getTrack().getLatRange();
85 DoubleRange lonRange = inTrackInfo.getTrack().getLonRange();
86 // Build basic url using min and max lat and long
87 String url = "http://openstreetmap.org/?minlat=" + FIVE_DP.format(latRange.getMinimum())
88 + "&maxlat=" + FIVE_DP.format(latRange.getMaximum())
89 + "&minlon=" + FIVE_DP.format(lonRange.getMinimum()) + "&maxlon=" + FIVE_DP.format(lonRange.getMaximum());
90 DataPoint currPoint = inTrackInfo.getCurrentPoint();
91 // Add selected point, if any (no way to add point name?)
92 if (currPoint != null) {
93 url = url + "&mlat=" + FIVE_DP.format(currPoint.getLatitude().getDouble())
94 + "&mlon=" + FIVE_DP.format(currPoint.getLongitude().getDouble());
100 * Get the median value from the given lat/long range
101 * @param inRange range of values
102 * @return median value
104 private static double getMedianValue(DoubleRange inRange)
106 return (inRange.getMaximum() + inRange.getMinimum()) / 2.0;
110 * Get the span of the given lat/long range
111 * @param inRange range of values
114 private static double getSpan(DoubleRange inRange)
116 return inRange.getMaximum() - inRange.getMinimum();