/WhereAbout/src/com/google/marvin/whereabout/BusinessLocator.java

http://eyes-free.googlecode.com/ · Java · 101 lines · 82 code · 9 blank · 10 comment · 11 complexity · e70aa32cd8adab49e88ba881d6719afd MD5 · raw file

  1. package com.google.marvin.whereabout;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.net.URL;
  5. import java.util.StringTokenizer;
  6. import org.htmlparser.Node;
  7. import org.htmlparser.Parser;
  8. import org.htmlparser.filters.HasAttributeFilter;
  9. import org.htmlparser.util.NodeList;
  10. import org.htmlparser.util.ParserException;
  11. import android.util.Log;
  12. public class BusinessLocator {
  13. // URL for obtaining businesses
  14. private static final String URL_BUSINESS =
  15. "http://maps.google.com/maps?q=*%20loc:";
  16. /** Private Constructor for this utility class */
  17. private BusinessLocator() {
  18. }
  19. public static String[] getBusinesses(double lat, double lon) {
  20. String[] bsList = null;
  21. try {
  22. String html = HttpUtil.getResult(makeBusinessURL(lat, lon));
  23. Parser p = new Parser();
  24. p.setInputHTML(html);
  25. NodeList buss = p.extractAllNodesThatMatch(
  26. new HasAttributeFilter("class", "name lname"));
  27. Log.d("Business", "Businesses: " + buss.size());
  28. int len = buss.size() / 2;
  29. bsList = new String[len];
  30. Business bs = new Business();
  31. for (int i = 0; i < len; i++) {
  32. Node n = buss.elementAt(i);
  33. String tmp = n.getFirstChild().getFirstChild().getText();
  34. tmp = tmp.substring(tmp.indexOf("latlng=") + 7);
  35. StringTokenizer st = new StringTokenizer(tmp, ",");
  36. bs.lat = Double.parseDouble(st.nextToken()) / 1000000;
  37. bs.lon = Double.parseDouble(st.nextToken()) / 1000000;
  38. bs.title = n.getFirstChild().getFirstChild().getFirstChild()
  39. .getFirstChild().getText();
  40. Log.d("Business", bs.title);
  41. NodeList tmpList = n.getNextSibling().getChildren();
  42. int tmpListLen = tmpList.size();
  43. for (int k = 0; k < tmpListLen; k++) {
  44. Node infoNode = tmpList.elementAt(k);
  45. if (infoNode.getText().indexOf("sxaddr") >= 0) {
  46. NodeList aNodes = infoNode.getChildren();
  47. int aNodesLen = aNodes.size();
  48. String address = "";
  49. for (int j = 0; j < aNodesLen; j++) {
  50. Node addrSubNode = aNodes.elementAt(j);
  51. if (addrSubNode.getText().indexOf("span") >= 0) {
  52. address += addrSubNode.getFirstChild().getText() + " ";
  53. }
  54. bs.address = address;
  55. }
  56. } else if (infoNode.getFirstChild() != null &&
  57. infoNode.getText().indexOf("span") >= 0) {
  58. Node tmpNode = infoNode.getFirstChild().getFirstChild();
  59. if (tmpNode != null) {
  60. bs.tel = tmpNode.getText();
  61. } else {
  62. bs.dir = infoNode.getFirstChild().getText();
  63. }
  64. }
  65. }
  66. bsList[i] = bs.title + "$" + bs.address + "$" + bs.tel + "$" +
  67. bs.lat + "$" + bs.lon + "$" + bs.dir;
  68. Log.d("Business", bsList[i]);
  69. }
  70. } catch (IOException e) {
  71. Log.d("Business", "Error reading from Map server: " + e.getMessage());
  72. Log.d("Business", "Trace: " + Log.getStackTraceString(e.fillInStackTrace()));
  73. } catch (ParserException pce) {
  74. Log.d("Business", "Could not parse HTML: " + pce.toString());
  75. }
  76. return bsList;
  77. }
  78. /**
  79. * Prepares the URL to connect to the Google Maps server to obtain list of
  80. * businesses around the specified lat and long.
  81. * @param lat latitude in degrees of the location to reverse geocode
  82. * @param lon longitude in degrees of the location to reverse geocode
  83. * @return
  84. * @throws MalformedURLException
  85. */
  86. private static URL makeBusinessURL(double lat, double lon)
  87. throws MalformedURLException {
  88. StringBuilder url = new StringBuilder();
  89. url.append(URL_BUSINESS).append(lat).append(",").append(lon);
  90. return new URL(url.toString());
  91. }
  92. }