0

Possible Duplicate:
How to get complete address from latitude and longitude?

How to get location name in Android using Lattitude and Longitude till the City Name and street name

Community
  • 1
  • 1
Mahavir
  • 1
  • 1
  • 2

3 Answers3

2

try this

private void getAddressGoogleQuery() {
        String address = "";
        try {
            String URL = "http://maps.google.com/maps/geo?q=" + latitude + ","
                    + longitude + "&output=csv";

            String device_address = Get(URL);
            String[] output = device_address.split("\"");

            try {
                address = output[1];
            } catch (Exception e) {
                // TODO: handle exception
            }
            Log.d("Activity", "Address:" + address);
        } catch (Exception e) {
        }

    }

 public static String Get(String URLStr) throws Exception 
 {


        String resultStr = "";
        BufferedReader in = null;
        try 
        {

            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(URLStr);
            HttpResponse response = client.execute(request);
            in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            resultStr = sb.toString();
            }
            catch(Exception ee)
            {
                ee.printStackTrace();
            }
            finally 
            {
                if (in != null) 
                {
                    try 
                    {
                        in.close();

                    } catch (Exception e) 
                    {
                    }
                }

            }
        return resultStr;
 }
Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40
0

You can achieve it using reverse geocoder. A sample code for that:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(latValue,
                    longValue, 1);

            if (addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder(
                        "Address:\n");
                for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                    strReturnedAddress
                            .append(returnedAddress.getAddressLine(i)).append(
                                    "\n");
                }
                myAddress.setText(strReturnedAddress.toString());
            } else {
                myAddress.setText("No Address returned!");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            myAddress.setText("Canont get Address!");
        }

Just place this code after you are getting latitude and longitude values.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

You can use the Geocoder class to get information such as city and country information.

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
keithmaxx
  • 649
  • 7
  • 17