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