Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse Geocode in google maps api 2 android

I want to do reverse geocoding in my app using map api 2.But i dont know exactly how to do that?Any ideas?

like image 437
Dimitris Avatar asked Mar 03 '13 21:03

Dimitris


2 Answers

Use Geocoder:

Geocoder geoCoder = new Geocoder(context);
List<Address> matches = geoCoder.getFromLocation(latitude, longitude, 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
like image 88
ianhanniballake Avatar answered Sep 22 '22 05:09

ianhanniballake


You can do reverse-geocoding in two ways

  • Geocoder
  • Google API

Geocoder It should be executed in a separate thread:

private class FindPlaces extends AsyncTask<String, Void, List<Address>> {

    @Override
    protected List<Address> doInBackground(String... params) {
        if (act == null)
            this.cancel(true);
        Geocoder geocoder = new Geocoder(act, Locale.getDefault());
        try {
            addresses = geocoder.getFromLocation(
                    Double.parseDouble(params[0]),
                    Double.parseDouble(params[1]), result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address> addresses) {
        super.onPostExecute(addresses);
        if (act == null)
            return;
        if (addresses == null || addresses.size() == 0) {
            Toast.makeText(act, "No location found", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
        address = addresses.get(0);

        String aLine = "";
        for (int addr = 0; addr <= address.getMaxAddressLineIndex() - 2; addr++) {
            aLine = aLine.length() > 0 ? aLine + ", "
                    + String.valueOf(address.getAddressLine(addr)) : String
                    .valueOf(address.getAddressLine(addr));
        }
        address.setAddressLine(0, aLine);
        if (act == null)
            return;

    }
}

Google API 1)Enable Google Maps Geocoding API in the google console 2)Concatenate your latlong in this url https://maps.googleapis.com/maps/api/geocode/json?latlng=

Eg: https://maps.googleapis.com/maps/api/geocode/json?latlng=youlatitude,yourlongitude&key=yourapikey

Call the below asyntask with your latlog This works for me..

public class ReverseGecoding extends AsyncTask<Double, Void, String> {

Context context;**
private Address address;
private String GEOCODINGKEY = "&key=YourKey";
private String REVERSE_GEOCODING_URL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=";

public ReverseGecoding(Context context) {
    this.context = context;
    this.listener = listener;
}

@Override
protected String doInBackground(Double... params) {
    if (params[0] != null) {
         String result = "";
    try {
        String mUrl = REVERSE_GEOCODING_URL + params[0] + ","
                + params[1] + GEOCODINGKEY;

        URL url = new URL(mUrl);
        HttpURLConnection httpsURLConnection = (HttpURLConnection) url.openConnection();
        httpsURLConnection.setReadTimeout(10000);
        httpsURLConnection.setConnectTimeout(15000);
        httpsURLConnection.setDoInput(true);
        httpsURLConnection.setRequestMethod("GET");
        httpsURLConnection.connect();
        int mStatus = httpsURLConnection.getResponseCode();
        if (mStatus == 200)
            return readResponse(httpsURLConnection.getInputStream()).toString();
        return result;

    } catch (Exception e) {
        e.printStackTrace();

    }
    return null;

}



private static StringBuilder readResponse(InputStream inputStream) throws IOException, NullPointerException {
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            stringBuilder.append(line);
        }
        return stringBuilder;
    }
}
like image 30
Raja Jawahar Avatar answered Sep 25 '22 05:09

Raja Jawahar