Get the address using current latitude and longitude


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

?
1
2
3
4
5
6
7
8
9
10
<linearlayout android:gravity="center" android:id="@+id/LinearLayout1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
 
    <edittext android:ems="10" android:id="@+id/editText_result" android:layout_height="wrap_content" android:layout_width="match_parent">
 
        <requestfocus>
    </requestfocus></edittext>
 
    <button android:id="@+id/button_getAddress" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Get Address">
 
</button></linearlayout>

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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<address>
 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<string string="" void=""> {
 
  @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);
  
  }
 }
}
 
</string></address>


Comments

  1. How To Make Money From Gambling on Bet365 | Work-to-Earn
    › work-to-Earn › work-to-Earn Bet365 is หาเงินออนไลน์ a fantastic sports betting platform, available both live and in-play, and sports betting on all major leagues and major sports.

    ReplyDelete

Post a Comment

Popular posts from this blog