As you know, lot of things got easier with the invention of Google Maps i.e.; we can locate a particular house/shop/company etc. I'm not sure whether you're aware hot it works? It all works based on the latitude and longitude points/values. Latitude represents the north-south point on the earth and Longitude represents the east-west point on the earth.

There are several APIs to get the latitude and longitude values programatically. But I've developed a small standalone application which gets the latitude and longitude values from Google maps.

It's very simple, all I'm doing is - polling the Google maps URL with the post code/search key and reading the response received, if it's successful.

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.Document;

* This class will get the lat long values.
* @version 1.0
* @since 20-Sep-2012
public class LatLng
public static void main(String[] args) throws Exception
BufferedReader reader = new BufferedReader(new InputStreamReader(;
System.out.println("Please enter a location:");
String postcode=reader.readLine();
String latLongs[] = getLatLongPositions(postcode);
System.out.println("Latitude: "+latLongs[0]+" and Longitude: "+latLongs[1]);

public static String[] getLatLongPositions(String address) throws Exception
int responseCode = 0;
String api = "" + URLEncoder.encode(address, "UTF-8") + "&sensor=true";
URL url = new URL(api);
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
responseCode = httpConnection.getResponseCode();
if(responseCode == 200)
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();;
Document document = builder.parse(httpConnection.getInputStream());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/GeocodeResponse/status");
String status = (String)expr.evaluate(document, XPathConstants.STRING);
expr = xpath.compile("//geometry/location/lat");
String latitude = (String)expr.evaluate(document, XPathConstants.STRING);
expr = xpath.compile("//geometry/location/lng");
String longitude = (String)expr.evaluate(document, XPathConstants.STRING);
return new String[] {latitude, longitude};
throw new Exception("Error from the API - response status: "+status);
return null;
>java LatLng
Please enter a location:
600017, Chennai
Latitude: 13.0412658 and Longitude: 80.2338514

Please put your comments and questions below.

If you've sometime, why don't you checkout my other Java programs