1

I am building an android application Where user select the area using google map.

Now I am getting lat long and I am parsing the Lat long in an function to get the address.

Here is the code

public void getMyLocationAddress() {

    Geocoder geocoder= new Geocoder(getApplicationContext(), Locale.getDefault());
     List<Address> addresses = null;
    try {

          //Place your latitude and longitude
         addresses = geocoder.getFromLocation(mUpCameraPosition.target.latitude,mUpCameraPosition.target.longitude, 1);  
          if(addresses != null) {

              Address fetchedAddress = addresses.get(0);
              StringBuilder strAddress = new StringBuilder();

              for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
                    strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
              }

              completed = strAddress.toString();
              whitebord.setText(completed);

          }               
             whitebord.setText("No location found..!");
    } 
    catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             Toast.makeText(getApplicationContext(),"Could not get address..!",  
             Toast.LENGTH_LONG).show();
    }
}
Neal
  • 194
  • 3
  • 15

4 Answers4

3

Try this, It will help you.

I have edited code. may be your device may not support Geocoder. Use Google API to get address

try{
Geocoder geocoder = new Geocoder(SignIn.this,
                    Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(mlatitiude,
                    mlongitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                mCountryName = address.getCountryName();
                mLocationName = address.getLocality();

            }
  } catch (IOException e) {
  HttpClient httpclient = new DefaultHttpClient();
            String postURL = "http://maps.googleapis.com/maps/api/geocode/json?latlng="
                    + mlatitiude + "," + mlongitude + "&sensor=true";
            HttpGet httppost = new HttpGet(postURL);
            try {

                HttpResponse response = httpclient.execute(httppost);
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity()
                                .getContent()));
                String line = "";
                StringBuilder sb = new StringBuilder();
                while ((line = rd.readLine()) != null) {
                    sb.append(line + "\n");
                }
                String result = sb.toString();

                JSONObject jobj = new JSONObject(result);
                JSONArray array = jobj.getJSONArray("results")
                        .getJSONObject(0)
                        .getJSONArray("address_components");
                int size = array.length();
                for (int i = 0; i < size; i++) {
                    JSONArray typearray = array.getJSONObject(i)
                            .getJSONArray("types");

                    if (typearray.getString(0).equals("country")) {
                        mCountryName = array.getJSONObject(i).getString(
                                "long_name");

                    }
                    if (typearray.getString(0).equals("locality")) {
                        mLocationName = array.getJSONObject(i).getString(
                                "long_name");

                    }

                }

            } catch (Exception e1) {
                // TODO: handle exception

            }
  }
Murali Ganesan
  • 2,925
  • 4
  • 20
  • 31
  • Still It is directly goes to catch – Neal Nov 27 '14 at 04:42
  • Thanks, It work now can You please tell How to get an particular location from it – Neal Nov 27 '14 at 05:44
  • Check that code. if (typearray.getString(0).equals("country")) { mCountryName = array.getJSONObject(i).getString( "long_name"); You will get country name and location name } if (typearray.getString(0).equals("locality")) { mLocationName = array.getJSONObject(i).getString( "long_name"); – Murali Ganesan Nov 27 '14 at 08:47
  • Ok Thanks.. let me try and get back to you soon – Neal Nov 27 '14 at 09:32
0

This is code snippet from answer of same question given here

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

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
Community
  • 1
  • 1
Android Noob
  • 621
  • 8
  • 18
0

Use this:

private void getAddressFromLocation(final LatLng latlng) {


        new Thread(new Runnable() {

            @Override
            public void run() {

                Geocoder gCoder = new Geocoder(this);
                try {
                    final List<Address> list = gCoder.getFromLocation(
                            latlng.latitude, latlng.longitude, 1);
                    if (list != null && list.size() > 0) {
                        Address address = list.get(0);
                        StringBuilder sb = new StringBuilder();
                        if (address.getAddressLine(0) != null) {
                            sb.append(address.getAddressLine(0)).append("\n");
                        }
                        sb.append(address.getLocality()).append(",");
                        sb.append(address.getPostalCode()).append(",");
                        sb.append(address.getCountryName());
                        String strAddress = sb.toString();

                    }

                } catch (IOException exc) {
                    exc.printStackTrace();
                }
            }
        }).start();

    }

It is already answered here : https://stackoverflow.com/a/22324307/2655804

Community
  • 1
  • 1
Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
  • Thanks let me try and get back to you – Neal Nov 27 '14 at 05:24
  • can you please tell how to pares the lat long I am getting from this code - @Override public void onTouchUp(MotionEvent event) { mUpCameraPosition = getMap().getMap().getCameraPosition(); getMap().getMap().clear();// to remove previous marker MarkerOptions options = new MarkerOptions().title("This is your selected place to host").position( new LatLng(mUpCameraPosition.target.latitude, mUpCameraPosition.target.longitude)); getAddressFromLocation(null); } – Neal Nov 27 '14 at 05:30
  • pass your latlong to this method using getAddressFromLocation(new LatLang(yourLat,yourLong)). – Kishan Dhamat Nov 27 '14 at 05:41
0

Pass your latitude and longitude values here.

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(getContext(), Locale.getDefault());

    try {
        addresses = geocoder. getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
        String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
        String city = addresses.get(0).getLocality();
        String state = addresses.get(0).getAdminArea();
        String country = addresses.get(0).getCountryName();
        String postalCode = addresses.get(0).getPostalCode();
        String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL

        System.out.println(address+"-------------");
    } catch (IOException e) {
        e.printStackTrace();
    }
Marium Jawed
  • 391
  • 4
  • 9