How to get json object of google maps places api in android? -
is there way json data google maps places api in android. using following link 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 me how can data form above in android.
i know it's old question. hope useful others in future.
first, have add module level build.gradle
,
android { uselibrary 'org.apache.http.legacy' }
next step create 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; // 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 execute asynctask wherever want. (maybe 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 google places, example keyword, language etc. read more options , how use them here.
Comments
Post a Comment