Friday, 5 July 2013

Android How to Get Address From Google Services.

Hi All,
By this post we are going to learn about how to get Address from Google Services instead of Default Geo Coder.

Step 1. 
For this we need Internet Permission.

<uses-permission android:name="android.permission.INTERNET"/>

Step 2.
Here is the Class to get The Address from Google Services.


import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class FormattedAddress {

    public  String getAddress(String Source, String Destination) {

       
       
        StringBuilder stringBuilder = new StringBuilder();
        String duration_final_value = "";
       
        try {

            Source = Source.replaceAll(" ", "%20");
            Destination = Destination.replaceAll(" ", "%20");

            HttpPost httppost = new HttpPost(
                    "http://maps.googleapis.com/maps/api/geocode/json?latlng="+Source+","+Destination+"&sensor=false");//&mode=walking,driving
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            stringBuilder = new StringBuilder();

            response = client.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {         
        } catch (IOException e) {
        }

       
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString()); // first we need to get the full json object which google returns   
           
            JSONArray routes_Array = jsonObject.getJSONArray("results"); // in that first we need to go into the routes array
           
            JSONObject routes_object = (JSONObject) routes_Array.get(0); // in that we need first item
            duration_final_value= routes_object.getString("formatted_address").toString();  // then go into the legs array
           
            System.out.println(" Address " + duration_final_value);
               

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return duration_final_value;
    }
}

Step 3:
Just call the above public method from your activity like below by passing the Latitude and Longitude

              FormattedAddress tel=new FormattedAddress();
            foundAddress= tel.getAddress(String.valueOf(latitude), String.valueOf(longitude));
            here latitude, longitude are long values.

hope this helps you.
Happy Coding.

2 comments:

Unknown said...

I simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.
Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery

Blogger said...

If you would like an alternative to casually dating girls and trying to figure out the right thing to do...

If you would prefer to have women chase YOU, instead of spending your nights prowling around in noisy bars and night clubs...

Then I encourage you to watch this eye-opening video to discover a strange little secret that might get you your very own harem of sexy women:

FACEBOOK SEDUCTION SYSTEM!!!