Thursday, October 15, 2009

Searching Nearby in Android

Using google's geocoder in android is not much fun. It does your basic conversion between coordinates and addresses but to find a business nearby specific coordinates, is a whole different game altogether. From what I gather, there are two ways to get this done:

1) I've heard the Yahoo geocoder library has this functionality but who uses Yahoo to find a starbucks anyway?
2) Query Google Maps for the information

Im gonna discuss option 2. Steps involved are: query google maps, get our data, parse.

First we create a URL object:

URL url = new URL("http://maps.google.com/maps?q=your+query+here");

SIDENOTE:
the 'your+query+part' is where you want to put what you want to find. Its really simple... play around with the website and you'll see the URL in the browser change everytime you type in an address or search for something. A simple example is when I want to search for food near some latitude & longitude, my query string will be:

q=food+loc:+latitude+longitude

Don't forget to use + for spaces, and latitude & longitude are in double format. You also would want to get the information back in some form so you could parse through it. To achieve this, add the 'output' variable to your query string. Goto http://mapki.com/wiki/Google_Map_Parameters for an explanation of all the different paramaters and their usage. So finally, our URL becomes:


"http://maps.google.com/maps?q=food+loc:latitude+longitude&output=kml" (for a kml output)


Back to the code:

URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream(); // Google will send you the KML data via this input stream

From here on out I don't really understand how the tree is built, what nodes and node elements are, but I could extract the information I was looking for with the following lines of code:

// Now to build the document tree by parsing the inputStream.
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
document.getDocumentElement().normalize();

// Each result of your query is placed in a placemark tag
NodeList
placemarks = document.getElementsByTagName("Placemark");



for (Node item : placemarks)
if (item.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) item;

String tagName = "name"; // Name of each placemark or business
NodeList childNodes = element.getElementByTagName(tagName);
Element nameElement = childNodes.item(0);
String tagValue = nameElement.getChildNodes().item(0).getNodeValue();
}



To retrive additional information from your KML, substitute tagName with:

"address" - to get address of each result. tagValue will then contain the html form of the address, meaning that each address line will be separated with <br/>. You can use the replaceAll(String, String) method in the String class to replace all instances of <br/> with "\n" or maybe ",".

"coordinates" - gets the latitude and longitude of each result.

"Snippet" - this gets you the phone number but after an additional step. Use the split(String) method in the String class to split tagValue into an array of sub-strings at <br/> and the phone number will ALWAYS be the second item in the resulting array.

And that's it! Now you can search for stuff around you using the GPS and querying Google. The only downside to this technique the cost involved in transferring the data back to your phone over the network.

2 comments:

  1. Hey varun...this is sunil here....

    I have a question that, for finding nearby Do we need to provide lat-long?

    ReplyDelete
  2. I mean as prof whitney told us to send lat-long using DDMS..so we have to do in that way or google can get current location automatically?

    ReplyDelete