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.