Monday, February 15, 2016

How to get string adress from latitude and longitude in android (Reverse geocoding)


Fetch address data using  GEOCODER(Reverse geocoding) and if it fails ,we can also use GOOGLE API to get address from lat lng.

What is Geocoding?

Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers on a map, or position the map.



Reverse geocoding is the process of converting geographic coordinates into a human-readable address. The Google Maps Geocoding API's reverse geocoding service also lets you find the address for a given place ID.


public class GetStringAddressFromLatLng extends AsyncTask<Void, Void, String>
{

    CallBackWeb callBackWeb;
    Context con;
    LatLng latLng;

    public GetStringAddressFromLatLng(CallBackWeb callBackWeb, Context con, LatLng latLng)
    {
        this.callBackWeb = callBackWeb;
        this.con = con;
        this.latLng = latLng;
    }

    @Override
    protected String doInBackground(Void... voids)
    {
        return GetAddressByString(con,latLng);
    }

    @Override
    protected void onPostExecute(String address)
    {

       //this is the address which you need........
        super.onPostExecute(s);
    }


    private String GetAddressByString(Context con, LatLng addressssss)
    {

        String addressReturn = "";
        Geocoder geocoder = new Geocoder(con, Locale.getDefault());

        List<Address> addresses;
        try
        {

     addresses = geocoder.getFromLocation(addressssss.latitude, addressssss.longitude, 3);
//
//            for (int i = 0; i < addresses.size(); i++) {

            if (addresses.isEmpty())
            {
                addressReturn = getLocationFromString(addressssss);
            }
            else
            {
                int getMAxAddrss = addresses.get(0).getMaxAddressLineIndex();

                for (int g = 0; g < getMAxAddrss; g++)
                {
                    addressReturn = addressReturn + "," + addresses.get(0).getAddressLine(g);
                }
                addressReturn = addressReturn.substring(1, addressReturn.length());
                // addrss[i] = addressReturn;
                return addressReturn;

            }


        }
        catch (Exception | Error e)
        {
            addressReturn = getLocationFromString(addressssss);

        }


        return addressReturn;

    }


    public String getLocationFromString(LatLng addressssss)
    {
        String addrsssssName = "";
        try
        {

            String URL = "http://maps.google.com/maps/api/geocode/json?latlng=" +
 URLEncoder.encode(addressssss.latitude + "," + addressssss.longitude, "UTF-8") +
 "&en&sensor=false";

            JSONObject jsonObject = new JSONObject(new WebServiceHelper().performGetCall(URL));      //hit webservice here

            JSONArray results = jsonObject.getJSONArray("results");


            if (results.length() > 0)
            {

                addrsssssName = results.getJSONObject(0).getString("formatted_address");
                return addrsssssName != null ? addrsssssName : "";


        }
        catch (Exception e)
        {
            return addrsssssName;
        }
        catch (Error e)
        {
            return addrsssssName;
        }

        return addrsssssName;
    }


}

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design