PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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