Posted on 9 Comments

Convert GPS Coordinates to Address using Google Geocoding API using Java

Google Logo

Google Maps makes it easy to convert addresses to their corresponding GPS coordinates using the Geocoding API. But what if you want to do the reverse, i.e. convert GPS coordinates to the corresponding address? Simple, just use Google’s ReverseGeocoding functionality.

Below you find a simple Java example of how to use the API to convert GPS coordinates to addresses:

package at.kerstner.geocoding;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

 * @param lng
 * @param lat
 * @return
    private String getAddressByGpsCoordinates(String lng, String lat)
            throws MalformedURLException, IOException, org.json.simple.parser.ParseException {
        URL url = new URL(""
                + lat + "," + lng + "&sensor=true");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        String formattedAddress = "";

        try {
            InputStream in = url.openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String result, line = reader.readLine();
            result = line;
            while ((line = reader.readLine()) != null) {
                result += line;

            JSONParser parser = new JSONParser();
            JSONObject rsp = (JSONObject) parser.parse(result);

            if (rsp.containsKey("results")) {
                JSONArray matches = (JSONArray) rsp.get("results");
                JSONObject data = (JSONObject) matches.get(0); //TODO: check if idx=0 exists
                formattedAddress = (String) data.get("formatted_address");

            return "";
        } finally {
            return formattedAddress;

The example above retrieves the formatted address of the first result found. Additional checks have been omitted for better readability.

The API response has the following format:

"formatted_address":"Entenplatz 1, 8020 Graz, Austria",



You also might want to checkout for an easy way to handle and convert coordinates and preview them on a map.

9 thoughts on “Convert GPS Coordinates to Address using Google Geocoding API using Java


  2. Thanks a lot friend!!!

    1. Can u plz tell which jars i have to use for this code

  3. will u plz send me the whole code?

  4. Thanks a lot….perfect.

    1. hi srikant could u please send me the code

  5. How i can process multiple latitude/longitude to get their respective address in a single request.
    Please suggest

  6. Server returned HTTP response code: 400 for URL:‎17.387140,78.491684&sensor=true
    at Source)
    at Source)
    at Source)

    1. i am getting this error. while using this code, can u help me ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.