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

http://eyes-free.googlecode.com/ · Java · 174 lines · 92 code · 15 blank · 67 comment · 8 complexity · 7dd5412a4342841fc7a1cbc349f6da8a 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.shell;
  17. import android.util.Log;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.net.HttpURLConnection;
  25. import java.net.MalformedURLException;
  26. import java.net.URL;
  27. /**
  28. * This class implements methods to get street address from lat-lon using
  29. * reverse geocoding API through HTTP.
  30. *
  31. * @author chaitanyag@google.com (Chaitanya Gharpure)
  32. */
  33. public class StreetLocator {
  34. public interface StreetLocatorListener {
  35. public void onAddressLocated(String address);
  36. }
  37. private StreetLocatorListener cb;
  38. private static final String ENCODING = "UTF-8";
  39. // URL for obtaining reverse geocoded location
  40. private static final String URL_GEO_STRING = "http://maps.google.com/maps/geo?";
  41. public StreetLocator(StreetLocatorListener callback) {
  42. cb = callback;
  43. }
  44. /**
  45. * Queries the map server and obtains the reverse geocoded address of the
  46. * specified location.
  47. *
  48. * @param lat The latitude in degrees
  49. * @param lon The longitude in degrees
  50. */
  51. public void getAddressAsync(double lat, double lon) {
  52. final double latitude = lat;
  53. final double longitude = lon;
  54. class AddressThread implements Runnable {
  55. @Override
  56. public void run() {
  57. String address = getAddress(latitude, longitude);
  58. if (address != null) {
  59. cb.onAddressLocated(address);
  60. }
  61. }
  62. }
  63. (new Thread(new AddressThread())).start();
  64. }
  65. /**
  66. * Queries the map server and obtains the reverse geocoded address of the
  67. * specified location.
  68. *
  69. * @param lat The latitude in degrees
  70. * @param lon The longitude in degrees
  71. * @return Returns the reverse geocoded address
  72. */
  73. public String getAddress(double lat, double lon) {
  74. try {
  75. String resp = getResult(makeGeoURL(lat, lon));
  76. JSONObject jsonObj = new JSONObject(resp);
  77. int code = jsonObj.getJSONObject("Status").getInt("code");
  78. if (code == 200) {
  79. return extendShorts(
  80. jsonObj.getJSONArray("Placemark").getJSONObject(0).getString("address"));
  81. }
  82. } catch (MalformedURLException mue) {
  83. Log.d("Locator", "Malformed URL: " + mue.getMessage());
  84. } catch (IOException e) {
  85. Log.d("Locator", "Error reading from Map server: " + e.toString());
  86. } catch (JSONException e) {
  87. Log.d("Locator", "Error in JSON response: " + e.getMessage());
  88. }
  89. return null;
  90. }
  91. /**
  92. * Sends a request to the specified URL and obtains the result from the
  93. * sever.
  94. *
  95. * @param url The URL to connect to
  96. * @return the server response
  97. * @throws IOException
  98. */
  99. private String getResult(URL url) throws IOException {
  100. Log.d("Locator", url.toString());
  101. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  102. conn.setDoInput(true);
  103. conn.setDoOutput(true);
  104. InputStream is = conn.getInputStream();
  105. String result = toString(is);
  106. return result;
  107. }
  108. /**
  109. * Prepares the URL to connect to the reverse geocoding server from the
  110. * specified location coordinates.
  111. *
  112. * @param lat latitude in degrees of the location to reverse geocode
  113. * @param lon longitude in degrees of the location to reverse geocode
  114. * @return URL The Geo URL created based on the given lat/lon
  115. * @throws MalformedURLException
  116. */
  117. private URL makeGeoURL(double lat, double lon) throws MalformedURLException {
  118. StringBuilder url = new StringBuilder();
  119. url.append(URL_GEO_STRING).append("q=").append(lat).append(",").append(lon);
  120. return new URL(url.toString());
  121. }
  122. /**
  123. * Reads an InputStream and returns its contents as a String.
  124. *
  125. * @param inputStream The InputStream to read from.
  126. * @return The contents of the InputStream as a String.
  127. */
  128. private static String toString(InputStream inputStream) throws IOException {
  129. StringBuilder outputBuilder = new StringBuilder();
  130. String string;
  131. if (inputStream != null) {
  132. BufferedReader reader = new BufferedReader(
  133. new InputStreamReader(inputStream, ENCODING));
  134. while (null != (string = reader.readLine())) {
  135. outputBuilder.append(string).append('\n');
  136. }
  137. }
  138. return outputBuilder.toString();
  139. }
  140. /**
  141. * Replaces the short forms in the address by their longer forms, so that
  142. * TTS speaks the addresses properly
  143. *
  144. * @param addr The address from which to replace short forms
  145. * @return the modified address string
  146. */
  147. private String extendShorts(String addr) {
  148. addr = addr.replace("St,", "Street");
  149. addr = addr.replace("St.", "Street");
  150. addr = addr.replace("Rd", "Road");
  151. addr = addr.replace("Fwy", "Freeway");
  152. addr = addr.replace("Pkwy", "Parkway");
  153. addr = addr.replace("Blvd", "Boulevard");
  154. addr = addr.replace("Expy", "Expressway");
  155. addr = addr.replace("Ave", "Avenue");
  156. addr = addr.replace("Dr", "Drive");
  157. return addr;
  158. }
  159. }