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 ?

Community
  • 1
  • 1
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
  • 1
    Have you gone through the tutorial link in the answer of the question [Google Places API in Android](http://stackoverflow.com/questions/3830149/using-google-places-api-in-android)? – Harry Joy Apr 22 '11 at 06:52
  • Yes. That's what i have wrote in question only. – Kartik Domadiya Apr 22 '11 at 06:53
  • What problem you face in following the tutorial? Do you get any error? – Harry Joy Apr 22 '11 at 06:56
  • @Harry Joy : i am not getting the terms given in UrlSigner class .. how to implement this thing ? – Kartik Domadiya Apr 22 '11 at 07:00
  • Which terms? `keyString` and `urlString`? – Harry Joy Apr 22 '11 at 07:01
  • @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 ? – Kartik Domadiya Apr 22 '11 at 07:04
  • @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 ? – Kartik Domadiya Apr 22 '11 at 07:05
  • `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. – Harry Joy Apr 22 '11 at 07:07
  • String urlString = "YOUR_URL_TO_SIGN"; ????? what thing should be included here ? – Kartik Domadiya Apr 22 '11 at 07:09
  • hey can you please elaborate what that code is doing ? – Kartik Domadiya Apr 22 '11 at 07:12
  • its the url you wanna sign by Google Map Key. The tutorial code is well commented to get understood. – Harry Joy Apr 22 '11 at 08:20
  • 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" } – Harsha Sep 18 '15 at 09:31

5 Answers5

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.

Andy
  • 49,085
  • 60
  • 166
  • 233
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • 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. – Kartik Domadiya Apr 28 '11 at 04:31
  • @Kartik: No. You just need to be **signed in** in gmail. – Harry Joy Apr 28 '11 at 04:34
  • 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=SIGNATURE` – lis Aug 30 '15 at 09:20
  • 8
    The link is outdated. – ljeabmreosn Dec 29 '16 at 23:13
  • 2
    link is directed to ads... it is spam..Delete it please – Samir Alakbarov Jun 04 '19 at 12:26
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 "|".

Deepukjayan
  • 3,525
  • 1
  • 20
  • 31
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

ddewaele
  • 22,363
  • 10
  • 69
  • 82
  • @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 explain – Rashmi.B Jul 07 '11 at 12:51
  • 1
    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. – ddewaele Jul 08 '11 at 05:53
  • 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? – Rashmi.B Jul 09 '11 at 12:28
  • 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. – ddewaele Jul 11 '11 at 05:05
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.

sarah.w.99
  • 19
  • 1
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();
    }  

}
Harshal Voonna
  • 349
  • 2
  • 12