so many gave so many answer but all missed out the most crucial part.Below is how i achieved this: You need an API Key.
https://developers.google.com/maps/documentation/android-api/signup
Just take time patiently go through the code.I used the inner class in fragment.
code:-
private class DownloadRawData extends AsyncTask<LatLng, Void, ArrayList<String>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=new ProgressDialog(getActivity());
progressDialog.setMessage("Loading........");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected ArrayList<String> doInBackground(LatLng... latLng) {
ArrayList<String> strings=retrieveData(latLng[0].latitude,latLng[0].longitude);
return strings;
}
@Override
protected void onPostExecute(ArrayList<String> s) {
super.onPostExecute(s);
if(progressDialog!=null)
progressDialog.dismiss();
LocationInfoDialog locationinfoDialog=new LocationInfoDialog(getActivity(),s);
locationinfoDialog.show();
locationinfoDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
locationinfoDialog.setCancelable(false);
}
}
private void init() {
address1 = "";
address2 = "";
city = "";
state = "";
country = "";
county = "";
PIN = "";
}
private String createUrl(double latitude, double longitude) throws UnsupportedEncodingException {
init();
return "https://maps.googleapis.com/maps/api/geocode/json?" + "latlng=" + latitude + "," + longitude + "&key=" + getActivity().getResources().getString(R.string.map_apiid);
}
private URL buildUrl(double latitude, double longitude) {
try {
Log.w(TAG, "buildUrl: "+createUrl(latitude,longitude));
return new URL(createUrl(latitude,longitude));
} catch (MalformedURLException e) {
e.printStackTrace();
Log.e(LOG_TAG, "can't construct location object");
return null;
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
private String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
if (scanner.hasNext()) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
}
public String getAddress1() { return address1; }
public String getAddress2() { return address2; }
public String getCity() { return city; }
public String getState() { return state; }
public String getCountry() { return country; }
public String getCounty() { return county; }
public String getPIN() { return PIN; }
private ArrayList<String> retrieveData(double latitude, double longitude) {
ArrayList<String> strings=new ArrayList<>();
try {
String responseFromHttpUrl = getResponseFromHttpUrl(buildUrl(latitude, longitude));
JSONObject jsonResponse = new JSONObject(responseFromHttpUrl);
String status = jsonResponse.getString("status");
if (status.equalsIgnoreCase("OK")) {
JSONArray results = jsonResponse.getJSONArray("results");
JSONObject zero = results.getJSONObject(0);
JSONArray addressComponents = zero.getJSONArray("address_components");
String formatadd= zero.getString("formatted_address");
for (int i = 0; i < addressComponents.length(); i++) {
JSONObject zero2 = addressComponents.getJSONObject(i);
String longName = zero2.getString("long_name");
JSONArray types = zero2.getJSONArray("types");
String type = types.getString(0);
if (!TextUtils.isEmpty(longName)) {
if (type.equalsIgnoreCase("street_number")) {
address1 = longName + " ";
} else if (type.equalsIgnoreCase("route")) {
address1 = address1 + longName;
} else if (type.equalsIgnoreCase("sublocality")) {
address2 = longName;
} else if (type.equalsIgnoreCase("locality")) {
// address2 = address2 + longName + ", ";
city = longName;
} else if (type.equalsIgnoreCase("administrative_area_level_2")) {
county = longName;
} else if (type.equalsIgnoreCase("administrative_area_level_1")) {
state = longName;
} else if (type.equalsIgnoreCase("country")) {
country = longName;
} else if (type.equalsIgnoreCase("postal_code")) {
PIN = longName;
}
}
}
strings.add(formatadd);
strings.add(address1);
strings.add(address2);
strings.add(city);
strings.add(county);
strings.add(state);
strings.add(country);
strings.add(PIN);
}
} catch (Exception e) {
e.printStackTrace();
}
return strings;
}
Initialized like this:-
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
googleMap.addMarker(markerOptions);
new DownloadRawData().execute(latLng);
}
});