Aditi Sharma
Aditi Sharma

Reputation: 75

Geocoding example not working

I have written a simple geocoding application but it is not working.I have given all necessary permissions.Please someone tell me where i am going wrong.Thanx in advance.

public class ForgeocdingActivity extends Activity {
    Geocoder gc;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText ed=(EditText)findViewById(R.id.editText1);
        Button b1=(Button)findViewById(R.id.button1);
        final String  to_add = ed.getText().toString();    
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {
                try
                {
                List<Address> address2 = gc.getFromLocationName(to_add,3);

                if(address2 != null && address2.size() > 0)
                {
                         double lat1 = address2.get(0).getLatitude();
                         double lng1 = address2.get(0).getLongitude();
                         Toast.makeText(getBaseContext(), "Lat:"+lat1+"Lon:"+lng1, Toast.LENGTH_SHORT).show();
                }      
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        });

    }
}

Upvotes: 0

Views: 1859

Answers (3)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Please write below code for that

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(currentlat, currentlng, 1);

Now the list of Address contains the closest known areas and test this code into real device.

Upvotes: 1

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

I think you can try to get latitude and longitude from address1. then you will get latitude and longitude from following code List addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

Upvotes: 0

Cata
Cata

Reputation: 11211

Here is a working sample of Geocoding:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
  try {
        List<Address> list = geocoder.getFromLocation(lat,long, 1);
        String address = list.get(0).getAddressLine(0) + ", " + list.get(0).getAddressLine(1) + ", " + list.get(0).getAddressLine(2);               
  } catch (IOException e) {
     e.printStackTrace();
  }

Where "this"= activity context, "long" = longitude, "lat" = latitude

Upvotes: 0

Related Questions