开发者

Using Google Places API

I want to develop an application in which i want to display information about places nearby (within 1km )the user by getting its current location..

Say for example i want to display information about Restaurants, Shopping Malls, Hospitals located within 1km related to current location of the android device..

I have gone through this link : Using Google Places API in开发者_运维知识库 Android.. But didnt get more.

Have anyone used Google Places API in android ?


Some points to keep in mind:

  • In order to use Google’s Places API, you will need to register and obtain a Google Map’s API Key.
  • In class UrlSigner in the tutorial the variable descriptions are: keyString => is your Google Map api key. urlString is the url you want to sign using the api key.
  • In code you will find inputKey and inputUrl. these 2 variables are just for testing purpose you can omit them if you want. You can directly write code as follows:

    URL url = new URL(urlString);

    UrlSigner signer = new UrlSigner(keyString);

    String request = signer.signRequest(url.getPath(),url.getQuery());

    System.out.println("Signed URL :" + url.getProtocol() + "://" + url.getHost() + request);

in main method of UrlSigner class.


The API works well in India(of-course will work anywhere in the globe). What you must go through is Google Places API. You can simply give a url request to google with user's current location and place-types that you would prefer.

The response may be either XML or Json as per your request. All you have to do is parse it out. You can get places around you upto 50 kms. An example for url request is

https://maps.googleapis.com/maps/api/place/search/xml?location=9.934866,76.267235&radius=50000&types=country%7Cairport%7Camusement_park%7Cbank%7Cbook_store%7Cbus_station%7Ccafe%7Ccar_rental%7Ccar_repair%7Cchurch%7Cdoctor%7Cfire_station%7Cfood%7Chindu_temple%7Chospital%7Clawyer%7Clibrary%7Cmosque%7Cmuseum%7Cpark%7Cparking%7Cpharmacy%7Cpolice%7Cpost_office%7Crestaurant%7Cschool%7Ctrain_station%7Czoo&sensor=true&key=your_API_Key

*Note: %7C is just "|".


The Google Places API went public a couple of weeks ago.

Using the Android compatible Google APIs Client Library for Java, you can use the Google Places API from an Android runtime.

The Google Places API REST API is well documented. For an example on using the Google APIs Client Library for Java and the Google Places API, checkout the following blog entry:

http://ddewaele.blogspot.com/2011/05/introducing-google-places-api.html


Keep in mind, that tutorial was written almost a year ago. The Places API has been launched to the public since then, so the registration process is now much simpler - see the documentation at: http://code.google.com/apis/maps/documentation/places/#Limits

There are also new features available now, including search by category, with over 100 different categories offered, and an Autocomplete service: http://code.google.com/apis/maps/documentation/places/autocomplete.html

In particular, the category search should help you finding restaurants, shopping malls & hospitals specifically.


/*
For google api key:-
login to your mail account
go to https://code.google.com/apis/console/
goto services tab
click on places api and google map api button. Fill the details
goto api access tab and copy your google api key
*/



package com.example.jstest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;

public class MainActivity extends Activity {
    private String googleAPIKey = "your google api key"; 
    DefaultHttpClient client;
    HttpResponse res;
    HttpGet req;
    InputStream in;
    JSONObject jsonobj;
    JSONArray resarray;
    String requesturl;
    HttpEntity jsonentity;

    final String TAG = getClass().getSimpleName();  


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requesturl="https://maps.googleapis.com/maps/api/place/search/json?radius=500&sensor=false&key="+googleAPIKey+"&location=13.01,74.79";

        System.out.println("Request "+requesturl);
        client=new DefaultHttpClient();
        System.out.println("hello");

        req=new HttpGet(requesturl);
        System.out.println("hello");

        try {
            res=client.execute(req);
            StatusLine status = res.getStatusLine();
            int code = status.getStatusCode();
            System.out.println(code);
            if(code!=200)
            {
                System.out.println("Request Has not succeeded");
                finish();
            }

            jsonentity=res.getEntity();
            in = jsonentity.getContent();

            jsonobj=new JSONObject(convertStreamToString(in));


            resarray = jsonobj.getJSONArray("results");

            if(resarray.length()==0){
            }
            else{
                int len=resarray.length();
                for(int j=0;j<len;j++)
                {
                    Toast.makeText(getApplicationContext(), resarray.getJSONObject(j).getString("name") , Toast.LENGTH_LONG).show();
                }
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }   
        catch (JSONException e) {
            e.printStackTrace();
        }   
    }

    private String convertStreamToString(InputStream in) {
        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();
    }  

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜