This tip is regarding Google Geocode which in itself is a part of Google Map API. Google has released V3 of Google Map API recently.
Let me first describe What is Geocoding ??
Geocoding is the process of converting addresses (like “1600 Amphitheatre Parkway, Mountain View, CA”) into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers or position the map. The Google Geocoding API provides a direct way to access a geocoder via an HTTP request. Additionally, the service allows you
to perform the converse operation (turning coordinates into addresses); this process is known as “reverse geocoding.”
A Geocoding API request must be of the following form:
http://maps.googleapis.com/maps/api/geocode/output?parameters
where output may be either of the following values: json (recommended) indicates output in JavaScript Object Notation (JSON)
xml indicates output as XML
and parameters will be in this form:: “address,city,state,zip code”. Here Google does not expect all address, city, state and zip you pass. If you pass only one among four it will
return you some result but then it would be difficult for you to choose an address because Google will return all possible addresses.
So its better to try to pass every parameter then it will return you exact match that you want. Here city, state and zip code is pretty clear but some may confused what to pass in address.
address is nothing just a combination of street_number and route. As for exm: addres may be “1600 Amphitheatre Parkway” .
Anyway now I come to the main subject of this tip that how you can request the Google Geocode through simple java code::
This is the java code that requests Google by an URL pasing all the parameter address,city,state,zip code.
Code:: import java.io.ByteArrayOutputStream; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.List; import org.apache.commons.io.IOUtils; (For this you need to add "commons-io-1.3.1.jar" in your project.) public Class GeocodeImplementation { /*Geocode request URL. Here see we are passing "json" it means we will get the output in JSON format. * You can also pass "xml" instead of "json" for XML output. * For XML output URL will be "http://maps.googleapis.com/maps/api/geocode/xml"; */ private static final String URL = "http://maps.googleapis.com/maps/api/geocode/json"; /* * Here the fullAddress String is in format like "address,city,state,zipcode". Here address means "street number + route" . * */ public String getJSONByGoogle(String fullAddress) { /* * Create an java.net.URL object by passing the request URL in constructor. * Here you can see I am converting the fullAddress String in UTF-8 format. * You will get Exception if you don't convert your address in UTF-8 format. Perhaps google loves UTF-8 format. * In parameter we also need to pass "sensor" parameter. * sensor (required parameter) — Indicates whether or not the geocoding request comes from a device with a location sensor. This value must be either true or false. */ URL url = new URL(URL + "?address=" + URLEncoder.encode(fullAddress, "UTF-8")+ "&sensor=false"); // Open the Connection URLConnection conn = url.openConnection(); //This is Simple a byte array output stream that we will use to keep the output data from google. ByteArrayOutputStream output = new ByteArrayOutputStream(1024); // copying the output data from Google which will be either in JSON or XML depending on your request URL that in which format you have requested. IOUtils.copy(conn.getInputStream(), output); //close the byte array output stream now. output.close(); return output.toString(); // This returned String is JSON string from which you can retrieve all key value pair and can save it in POJO. } } Now you can call getJSONByGoogle() by passing full address like:: getJSONByGoogle("1600 Amphitheatre Parkway,Mountain View,CA,94043"); For above method call you will get JSON output like this:: { "results" : [ { "address_components" : [ { "long_name" : "1600", "short_name" : "1600", "types" : [ "street_number" ] }, { "long_name" : "Amphitheatre Pkwy", "short_name" : "Amphitheatre Pkwy", "types" : [ "route" ] }, { "long_name" : "Mountain View", "short_name" : "Mountain View", "types" : [ "locality", "political" ] }, { "long_name" : "Santa Clara", "short_name" : "Santa Clara", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "California", "short_name" : "CA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "94043", "short_name" : "94043", "types" : [ "postal_code" ] } ], "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA", "geometry" : { "location" : { "lat" : 37.42182720, "lng" : -122.08424090 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 37.42317618029149, "lng" : -122.0828919197085 }, "southwest" : { "lat" : 37.42047821970849, "lng" : -122.0855898802915 } } }, "types" : [ "street_address" ] } ], "status" : "OK" } As you can see Google does not gives you only lattitude and longitude information for an address but it also normalizes your address correctly. We had passed : 1600 Amphitheatre Parkway,Mountain View,CA,94043 But see in above JSON the "formatted_address" key- value: "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA" There are so many key fields in above JSON output. To know the details about all the fields please visit: http://code.google.com/apis/maps/documentation/geocoding/ My purpose was just to let you know how you can pass Geocode request to Google in Java.