Tags

, , ,

Firstly about the Places API.

Google Places API:

Four basic Place requests are available from the API:

  • Place Searches return a list of nearby Places based on a user’s location or search string.
  • Place Details requests return more detailed information about a specific Place, including user reviews.
  • Place Actions allow you to supplement the data in Google’s Places Database with data from your application. Place Actions allow you to schedule Events, weight Place rankings by check-in data, or add and remove Places.
  • Places Autocomplete can be used to provide autocomplete functionality for text-based geographic searches, by returning Places as you type.

Note: The Result can be got as a JSON file or XML file depending on your needs.here I will be getting the result as a JSON.

You can refer more about Places API here.

Step by Step procedure for using the Places API:

Step 1:

Create a new Android Project and add the android.permission.INTERNET to the manifest.

Step 2:

Now we have to send a HTTP Get request to the Google servers and receive a JSON file.

For this Purpose we will be importing the following classes.


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

Step 3:

First we have to have the URL to which the request is being sent.


String requesturl="https://maps.googleapis.com/maps/api/place/search/json?radius=500&sensor=false&key=<your API key>&location=-33.8670522,151.1957362";

Create a HttpClient from which the request must be sent.


DefaultHttpClient client=new DefaultHttpClient();

Then we must construct the Http Request which is of GET method.


HttpGet req=new HttpGet(requesturl);

Then we must send the request to get a response.


HttpResponse res=client.execute(req);

Step 4:

After receiving the response we must extract the Entity from the HttpResponse. Then the JSONObject must be constructed from it. Since the JSONObject Constructor accepts only String we must convert the Contents of the HttpResponse into a String.

HttpEntity jsonentity=res.getEntity();
InputStream in=jsonentity.getContent();
JSONObject jsonobj=new JSONObject(convertStreamToString(in));

The procedure for converting Stream to String.

private String convertStreamToString(InputStream in) {
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new InputStreamReader(in));
		StringBuilder jsonstr=new StringBuilder();
		String line;
		try {
			while((line=br.readLine())!=null)
			{
				String t=line+"\n";
				jsonstr.append(t);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonstr.toString();
	}

Step 5:

Now is the time for Parsing.

The JSON returned is like this.

{
   "html_attributions" : [],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.86682180,
               "lng" : 151.19542210
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "96760d4544ecdaaf2e87565915638238ca960f20",
         "name" : "Pirrama Rd",
         "reference" : "CoQBcgAAAIWIC2AA6D0V4r5BAzZ_-hdqlmDQO0FBGRNU4SGWhIv5a1yycXYi9d4w7K6-JeaQU_ZWrbXW19RPgF6VN_5iO05BeDof1DNzeBpsoSvIvFwrjkzZMPelEPVijJDxdu7f1Dr3BgPEvnxwUJ5eWO64rL9UGKMlicOdB5GMrc4cJ-3REhAVrcb67mrvPduZh4R80gPAGhQH0w5Yjv0k32AdYsbJb2_ycX2Kug",
         "types" : [ "route" ],
         "vicinity" : "Pyrmont"
      }
   ]
}

So the Parser must be written to parse the JSON of above format.

JSONArray resarray=jsonobj.getJSONArray("results");

if(resarray.length()==0){
	//No Results
}
else{
        int len=resarray.length();
	for(int j=0;j<len;j++)
	{
		Toast.makeText(getApplicationContext(), resarray.getJSONObject(j).getString("name") , Toast.LENGTH_LONG).show();
	}
}

A Toast message is shown to the User about the returned place names.

Advertisements