0

Is there any way to get the json data from google maps places api in android. I am using the following link to retrieve data.

https://maps.googleapis.com/maps/api/place/textsearch/json?query=Store+in+Lahore&type=clothing_store&key=----

it returns data in json format. Please help me how can I get the data form the above in android.

1
  • add an example of the data returned so you can be helped out
    – user3137329
    Aug 25, 2015 at 8:41

3 Answers 3

2

I know it's an old question. But I hope it will be useful for others in future.

First, you have to add this to your module level build.gradle,

android {
    useLibrary 'org.apache.http.legacy'
}

Next step is to create an AsyncTask,

public class MyAsyncTask extends AsyncTask<String, Void, Boolean> {
    private JSONObject jsonObject;

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        System.out.println(jsonObject); //use jsonObject here
    }

    protected Boolean doInBackground(final String... args) {
        try {
            Looper.prepare();
            String latitude = args[0];
            String longitude = args[1];
            String radius = args[2];
            String name = args[3];
            String key = "YOUR_API_KEY_FOR_BROWSER";

            String uri = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" 
                    + "location=" + latitude + "," + longitude 
                    + "&radius=" + radius 
                    + "&name=" + name
                    + "&key="+ key; // you can add more options here

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setEntity(new UrlEncodedFormEntity(new ArrayList<NameValuePair>()));
            HttpEntity httpEntity = httpClient.execute(httpPost).getEntity();

            InputStream stream = httpEntity.getContent();
            BufferedReader bReader = new BufferedReader(new InputStreamReader(stream, "utf-8"), 8);
            StringBuilder sBuilder = new StringBuilder();

            String line = null;
            while ((line = bReader.readLine()) != null) {
                sBuilder.append(line + "\n");
            }

            stream.close();
            jsonObject = new JSONObject(sBuilder.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

And then execute the AsyncTask from wherever you want. (Maybe from MainActivity),

String latitude = String.valueOf(latLng.latitude);
String longitude = String.valueOf(latLng.longitude);
String radius = "2000"; // 2 Kilometer
String name = "hospital";

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute(latitude, longitude, radius, name);

You can add more option while fetching places from Google Places, for example keyword, language etc. Read more about those options and how to use them here.

2
  • Thanks alot. It'ill be helpful for others :) Jan 29, 2016 at 20:44
  • Nice. it's not related, but you said "Maybe from MainActivity", how to do that exactly? I've tried to .excute(), but I don't know how to get the return value of onPostExecute().
    – Dohab
    Aug 14, 2017 at 11:45
0

Maybe GSON can help you about that.

0

Check out JSONObject and JSON Parser Tutorial for JSON data retrieval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.