/there/src/com/google/marvin/there/StreetLocator.java

http://eyes-free.googlecode.com/ · Java · 429 lines · 254 code · 38 blank · 137 comment · 23 complexity · da703632fa7965fc15433e6c108933c5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.marvin.there;
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.net.HttpURLConnection;
  22. import java.net.MalformedURLException;
  23. import java.net.URL;
  24. import java.util.HashSet;
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27. import android.location.Location;
  28. import android.location.LocationManager;
  29. import android.util.Log;
  30. /**
  31. * This class implements methods to get street address from lat-lon using
  32. * reverse geocoding API through HTTP.
  33. *
  34. * @author chaitanyag@google.com (Chaitanya Gharpure)
  35. */
  36. public class StreetLocator {
  37. public interface StreetLocatorListener {
  38. public void onIntersectionLocated(String[] streetnames);
  39. public void onAddressLocated(String address);
  40. public void onFrontBackLocated(String[] streetsFront, String[] streetsBack);
  41. }
  42. private StreetLocatorListener cb;
  43. private static final String ENCODING = "UTF-8";
  44. // URL for obtaining navigation directions
  45. private static final String URL_NAV_STRING = "http://maps.google.com/maps/nav?";
  46. // URL for obtaining reverse geocoded location
  47. private static final String URL_GEO_STRING = "http://maps.google.com/maps/geo?";
  48. public StreetLocator(StreetLocatorListener callback) {
  49. cb = callback;
  50. }
  51. /**
  52. * Queries the map server and obtains the street names at the specified
  53. * location. This is done by obtaining street name at specified location, and
  54. * at locations X meters to the N, S, E, and W of the specified location.
  55. *
  56. * @param lat The latitude in degrees
  57. * @param lon The longitude in degrees
  58. */
  59. public void getStreetIntersectionAsync(double lat, double lon) {
  60. final double latitude = lat;
  61. final double longitude = lon;
  62. class IntersectionThread implements Runnable {
  63. public void run() {
  64. cb.onIntersectionLocated(getStreetIntersection(latitude, longitude));
  65. }
  66. }
  67. (new Thread(new IntersectionThread())).start();
  68. }
  69. /**
  70. * Queries the map server and obtains the street names at the specified
  71. * location. This is done by obtaining street name at specified location, and
  72. * at locations X meters to the N, S, E, and W of the specified location.
  73. *
  74. * @param lat The latitude in degrees
  75. * @param lon The longitude in degrees
  76. */
  77. public void getStreetsInFrontAndBackAsync(double lat, double lon, double heading) {
  78. final double latitude = lat;
  79. final double longitude = lon;
  80. final double direction = heading;
  81. class IntersectionThread implements Runnable {
  82. public void run() {
  83. getStreetsInFrontAndBack(latitude, longitude, direction);
  84. }
  85. }
  86. (new Thread(new IntersectionThread())).start();
  87. }
  88. /**
  89. * Queries the map server and obtains the reverse geocoded address of the
  90. * specified location.
  91. *
  92. * @param lat The latitude in degrees
  93. * @param lon The longitude in degrees
  94. */
  95. public void getAddressAsync(double lat, double lon) {
  96. final double latitude = lat;
  97. final double longitude = lon;
  98. class AddressThread implements Runnable {
  99. public void run() {
  100. cb.onAddressLocated(getAddress(latitude, longitude));
  101. }
  102. }
  103. (new Thread(new AddressThread())).start();
  104. }
  105. /**
  106. * Queries the map server and obtains the street names at the specified
  107. * location. This is done by obtaining street name at specified location, and
  108. * at locations X meters to the N, S, E, and W of the specified location.
  109. *
  110. * @param lat The latitude in degrees
  111. * @param lon The longitude in degrees
  112. * @return Returns the string array containing street names
  113. */
  114. public String[] getStreetIntersection(double lat, double lon) {
  115. HashSet<String> streets = new HashSet<String>();
  116. try {
  117. for (int i = 0; i < 5; i++) {
  118. // Find street address at lat-lon x meters to the N, S, E and W of
  119. // the given lat-lon
  120. String street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  121. if (street != null) {
  122. streets.add(street);
  123. }
  124. // get points 150m away, towards N, E, S, and W
  125. if (i < 4) {
  126. Location nextLoc = endLocation(lat, lon, i * 90, 15);
  127. lat = nextLoc.getLatitude();
  128. lon = nextLoc.getLongitude();
  129. }
  130. }
  131. } catch (MalformedURLException mue) {
  132. Log.d("Locator", "Malformed URL: " + mue.getMessage());
  133. } catch (IOException e) {
  134. Log.d("Locator", "Error reading from Map server: " + e.toString());
  135. } catch (JSONException e) {
  136. Log.d("Locator", "Error in JSON response: " + e.getMessage());
  137. }
  138. String[] st = new String[streets.size()];
  139. int i = 0;
  140. for (String s : streets) {
  141. st[i++] = s;
  142. }
  143. return st;
  144. }
  145. /**
  146. * Queries the map server and obtains the street names at the specified
  147. * location. This is done by obtaining street name at specified location, and
  148. * at locations X meters to the N, S, E, and W of the specified location.
  149. *
  150. * @param lat The latitude in degrees
  151. * @param lon The longitude in degrees
  152. */
  153. public void getStreetsInFrontAndBack(double lat, double lon, double heading) {
  154. HashSet<String> streetsFront = new HashSet<String>();
  155. HashSet<String> streetsBack = new HashSet<String>();
  156. double searchDistance = 15; // 15m (? - is there really a factor of 10 here)
  157. try {
  158. // Get the current street
  159. String street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  160. if (street != null) {
  161. streetsFront.add(street);
  162. streetsBack.add(street);
  163. }
  164. Log.i("Current street", "lat: " + Double.toString(lat) + ", lon: " + Double.toString(lon));
  165. // Get the street in front of the current street
  166. Location nextLoc = endLocation(lat, lon, heading, searchDistance);
  167. lat = nextLoc.getLatitude();
  168. lon = nextLoc.getLongitude();
  169. street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  170. if (street != null) {
  171. streetsFront.add(street);
  172. }
  173. Log.i("Front street", "lat: " + Double.toString(lat) + ", lon: " + Double.toString(lon));
  174. // Get the street behind the current street
  175. heading = heading + 180;
  176. if (heading >= 360){
  177. heading = heading - 360;
  178. }
  179. nextLoc = endLocation(lat, lon, heading, searchDistance);
  180. lat = nextLoc.getLatitude();
  181. lon = nextLoc.getLongitude();
  182. street = parseStreetName(getResult(makeNavURL(lat, lon, lat, lon)));
  183. if (street != null) {
  184. streetsBack.add(street);
  185. }
  186. Log.i("Back street", "lat: " + Double.toString(lat) + ", lon: " + Double.toString(lon));
  187. String[] sf = new String[streetsFront.size()];
  188. int i = 0;
  189. for (String s : streetsFront) {
  190. sf[i++] = s;
  191. }
  192. String[] sb = new String[streetsBack.size()];
  193. i = 0;
  194. for (String s : streetsBack) {
  195. sb[i++] = s;
  196. }
  197. cb.onFrontBackLocated(sf, sb);
  198. } catch (MalformedURLException e) {
  199. // TODO Auto-generated catch block
  200. e.printStackTrace();
  201. } catch (JSONException e) {
  202. // TODO Auto-generated catch block
  203. e.printStackTrace();
  204. } catch (IOException e) {
  205. // TODO Auto-generated catch block
  206. e.printStackTrace();
  207. }
  208. }
  209. /**
  210. * Queries the map server and obtains the reverse geocoded address of the
  211. * specified location.
  212. *
  213. * @param lat The latitude in degrees
  214. * @param lon The longitude in degrees
  215. * @return Returns the reverse geocoded address
  216. */
  217. public String getAddress(double lat, double lon) {
  218. try {
  219. String resp = getResult(makeGeoURL(lat, lon));
  220. JSONObject jsonObj = new JSONObject(resp);
  221. int code = jsonObj.getJSONObject("Status").getInt("code");
  222. if (code == 200) {
  223. return extendShorts(jsonObj.getJSONArray("Placemark").getJSONObject(0).getString("address"));
  224. }
  225. } catch (MalformedURLException mue) {
  226. Log.d("Locator", "Malformed URL: " + mue.getMessage());
  227. } catch (IOException e) {
  228. Log.d("Locator", "Error reading from Map server: " + e.toString());
  229. } catch (JSONException e) {
  230. Log.d("Locator", "Error in JSON response: " + e.getMessage());
  231. }
  232. return null;
  233. }
  234. /**
  235. * Parses the JSON response to extract the street name.
  236. *
  237. * @param resp The String representation of the JSON response
  238. * @return Returns the street name
  239. * @throws JSONException
  240. */
  241. private String parseStreetName(String resp) throws JSONException {
  242. JSONObject jsonObj = new JSONObject(resp);
  243. int code = jsonObj.getJSONObject("Status").getInt("code");
  244. if (code == 200) {
  245. return extendShorts(jsonObj.getJSONArray("Placemark").getJSONObject(0).getString("address"));
  246. }
  247. return null;
  248. }
  249. /**
  250. * Sends a request to the specified URL and obtains the result from the sever.
  251. *
  252. * @param url The URL to connect to
  253. * @return the server response
  254. * @throws IOException
  255. */
  256. private String getResult(URL url) throws IOException {
  257. Log.d("Locator", url.toString());
  258. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  259. conn.setDoInput(true);
  260. conn.setDoOutput(true);
  261. InputStream is = conn.getInputStream();
  262. String result = toString(is);
  263. return result;
  264. }
  265. /**
  266. * Prepares the URL to connect to navigation server, from the specified start
  267. * and end location coordinates
  268. *
  269. * @param lat1 Start location latitude in degrees
  270. * @param lon1 Start location longitude in degrees
  271. * @param lat2 End location latitude in degrees
  272. * @param lon2 End location longitude in degrees
  273. * @return a well-formed URL
  274. * @throws MalformedURLException
  275. */
  276. private URL makeNavURL(double lat1, double lon1, double lat2, double lon2)
  277. throws MalformedURLException {
  278. StringBuilder url = new StringBuilder();
  279. url.append(URL_NAV_STRING).append("hl=EN&gl=EN&output=js&oe=utf8&q=from%3A").append(lat1)
  280. .append(",").append(lon1).append("+to%3A").append(lat2).append(",").append(lon2);
  281. return new URL(url.toString());
  282. }
  283. /**
  284. * Prepares the URL to connect to the reverse geocoding server from the
  285. * specified location coordinates.
  286. *
  287. * @param lat latitude in degrees of the location to reverse geocode
  288. * @param lon longitude in degrees of the location to reverse geocode
  289. * @return URL The Geo URL created based on the given lat/lon
  290. * @throws MalformedURLException
  291. */
  292. private URL makeGeoURL(double lat, double lon) throws MalformedURLException {
  293. StringBuilder url = new StringBuilder();
  294. url.append(URL_GEO_STRING).append("q=").append(lat).append(",").append(lon);
  295. return new URL(url.toString());
  296. }
  297. /**
  298. * Reads an InputStream and returns its contents as a String.
  299. *
  300. * @param inputStream The InputStream to read from.
  301. * @return The contents of the InputStream as a String.
  302. */
  303. private static String toString(InputStream inputStream) throws IOException {
  304. StringBuilder outputBuilder = new StringBuilder();
  305. String string;
  306. if (inputStream != null) {
  307. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, ENCODING));
  308. while (null != (string = reader.readLine())) {
  309. outputBuilder.append(string).append('\n');
  310. }
  311. }
  312. return outputBuilder.toString();
  313. }
  314. /**
  315. * Replaces the short forms in the address by their longer forms, so that TTS
  316. * speaks the addresses properly
  317. *
  318. * @param addr The address from which to replace short forms
  319. * @return the modified address string
  320. */
  321. private String extendShorts(String addr) {
  322. addr = addr.replace("St,", "Street");
  323. addr = addr.replace("St.", "Street");
  324. addr = addr.replace("Rd", "Road");
  325. addr = addr.replace("Fwy", "Freeway");
  326. addr = addr.replace("Pkwy", "Parkway");
  327. addr = addr.replace("Blvd", "Boulevard");
  328. addr = addr.replace("Expy", "Expressway");
  329. addr = addr.replace("Ave", "Avenue");
  330. addr = addr.replace("Dr", "Drive");
  331. return addr;
  332. }
  333. /**
  334. * Computes the new location at a particular direction and distance from the
  335. * specified location using the inverse Vincenti formula.
  336. *
  337. * @param lat1 latitude of source location in degrees
  338. * @param lon1 longitude of source location in degrees
  339. * @param brng Direction in degrees wrt source location
  340. * @param dist Distance from the source location
  341. * @return the new location
  342. */
  343. private Location endLocation(double lat1, double lon1, double brng, double dist) {
  344. double a = 6378137, b = 6356752.3142, f = 1 / 298.257223563;
  345. double s = dist;
  346. double alpha1 = Math.toRadians(brng);
  347. double sinAlpha1 = Math.sin(alpha1), cosAlpha1 = Math.cos(alpha1);
  348. double tanU1 = (1 - f) * Math.tan(Math.toRadians(lat1));
  349. double cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)), sinU1 = tanU1 * cosU1;
  350. double sigma1 = Math.atan2(tanU1, cosAlpha1);
  351. double sinAlpha = cosU1 * sinAlpha1;
  352. double cosSqAlpha = 1 - sinAlpha * sinAlpha;
  353. double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
  354. double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
  355. double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
  356. double sigma = s / (b * A), sigmaP = 2 * Math.PI;
  357. double cos2SigmaM = 0, sinSigma = 0, deltaSigma = 0, cosSigma = 0;
  358. while (Math.abs(sigma - sigmaP) > 1e-12) {
  359. cos2SigmaM = Math.cos(2 * sigma1 + sigma);
  360. sinSigma = Math.sin(sigma);
  361. cosSigma = Math.cos(sigma);
  362. deltaSigma =
  363. B
  364. * sinSigma
  365. * (cos2SigmaM + B
  366. / 4
  367. * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM
  368. * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
  369. sigmaP = sigma;
  370. sigma = s / (b * A) + deltaSigma;
  371. }
  372. double tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
  373. double lat2 =
  374. Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1, (1 - f)
  375. * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp));
  376. double lambda =
  377. Math.atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
  378. double C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
  379. double L =
  380. lambda
  381. - (1 - C)
  382. * f
  383. * sinAlpha
  384. * (sigma + C * sinSigma
  385. * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
  386. Location l = new Location(LocationManager.GPS_PROVIDER);
  387. l.setLatitude(Math.toDegrees(lat2));
  388. l.setLongitude(lon1 + Math.toDegrees(L));
  389. return l;
  390. }
  391. }