Using Google Places API
Asked Answered
M

5

36

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 ?

Mi answered 5/4, 2011 at 11:24 Comment(12)
Have you gone through the tutorial link in the answer of the question Google Places API in Android?Retrocede
Yes. That's what i have wrote in question only.Mi
What problem you face in following the tutorial? Do you get any error?Retrocede
@Harry Joy : i am not getting the terms given in UrlSigner class .. how to implement this thing ?Mi
Which terms? keyString and urlString?Retrocede
@Harry Joy : yes. I am not able to get much in that code. What that code is exactly doing.?.. what is the need of inputUrl there ?Mi
@HarryJoy : my aim is to display nearby Restaurants, Hospitals blah blah.. how can i do that?. Is it possible to do the same thing without using Places API ?Mi
keyString is Google Map API Key and inputUrl is used for testing purpose. You can omit it and use url declared in urlString. keyString and urlString should be defined by you.Retrocede
String urlString = "YOUR_URL_TO_SIGN"; ????? what thing should be included here ?Mi
hey can you please elaborate what that code is doing ?Mi
its the url you wanna sign by Google Map Key. The tutorial code is well commented to get understood.Retrocede
am getting response like this:{ "error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 183.83.193.68, with empty referer", "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" }Agathy
R
25

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.

Retrocede answered 24/4, 2011 at 17:24 Comment(5)
i dont have google adsense account. Its compulsory to have google adsense account ?. When i was going thru some google's documents they recommended this.Mi
@Kartik: No. You just need to be signed in in gmail.Retrocede
I'm on the same issue now... what do I have to fill in for clientId and SIGNATURE? I've just got a google maps key... http://maps.google.com/maps/api/place/search/json?location=40.717859,-73.9577937&radius=1600&client=clientId&sensor=true_or_false&signature=SIGNATUREMarmoset
The link is outdated.Verrazano
link is directed to ads... it is spam..Delete it pleaseShanney
P
12

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 "|".

Posthaste answered 7/2, 2012 at 9:50 Comment(0)
M
4

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

Merciful answered 30/5, 2011 at 9:20 Comment(4)
@ddewaele... i went through the link for your blog and also downloaded the code. But I am not getting why there is no res, android manifest files and folders? And what is pom.xml. Why do you create it. Please explainPronounce
As mentioned in the answer, the sample only covers the Google APIs Client Library for Java and the Google Places API. It can be embedded in an Android application without many changes.Merciful
Thanks for the humble reply ddewaele..I am a beginner in android and I am working on a bank locator. Could you help me about how do I go next after detecting my current location?what are the prerequisites for developing this locator app?Pronounce
I would suggest writing a new question to clearly describe what you want to do, what you have already tried, and where it went wrong.Merciful
P
1

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.

Perfuse answered 19/5, 2011 at 0:30 Comment(0)
T
0
/*
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();
    }  

}
Tribasic answered 1/10, 2013 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.