Hi Guys,
Today I am sharing the code of getting the current address from the current latitude and longitude.
For implementing this concept we need to use the Geocoder. So here question is arises what is Geocoder?
Geocoder is a class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address.
The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code.
The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
More about the GeoCoder and related method visit here GeoCoder
You can download the source code GetAddress
Today I am sharing the code of getting the current address from the current latitude and longitude.
For implementing this concept we need to use the Geocoder. So here question is arises what is Geocoder?
Geocoder is a class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address.
The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code.
The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
More about the GeoCoder and related method visit here GeoCoder
activity_main.xml
MainActivity.java
package com.sunil.address;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.example.addressfromlatlong.R;
public class MainActivity extends Activity implements OnClickListener{
private EditText result;
private Button btngetAddress;
private Context context=null;
private ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
result=(EditText)findViewById(R.id.editText_result);
btngetAddress=(Button)findViewById(R.id.button_getAddress);
btngetAddress.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
dialog = ProgressDialog.show(context, "","Please wait..", true);
GetCurrentAddress currentadd=new GetCurrentAddress();
currentadd.execute();
}
public String getAddress(Context ctx, double latitude, double longitude) {
StringBuilder result = new StringBuilder();
try {
Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String locality=address.getLocality();
String city=address.getCountryName();
String region_code=address.getCountryCode();
String zipcode=address.getPostalCode();
double lat =address.getLatitude();
double lon= address.getLongitude();
result.append(locality+" ");
result.append(city+" "+ region_code+" ");
result.append(zipcode);
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
return result.toString();
}
private class GetCurrentAddress extends AsyncTask{
@Override
protected String doInBackground(String... urls) {
// this lat and log we can get from current location but here we given hard coded
double latitude=12.916523125961666;
double longitude=77.61959824603072;
String address= getAddress(context, latitude, longitude);
return address;
}
@Override
protected void onPostExecute(String resultString) {
dialog.dismiss();
result.setText(resultString);
}
}
}
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :