I am trying to make one app which will give the nearest hospital name and address. I am using Json parsing. Here is my code ..
I am using this url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=17.4367624,78.439968799&radius=5000&types=hospital&sensor=true&key=AIzaSyAwBKDflSlg9d38GX1xrwpnwcbWCzdVj-A"
I this longi and lati value is given manually but i want that where ever user will go, it should take the corresponding lat & long value according to the current location. Please help me how can i approach this...Any help will be appreciable.. Thank you..
public class MainActivity extends Activity {
JSONArray jsonArray;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=17.4367624,78.439968799&radius=5000&types=hospital&sensor=true&key=AIzaSyAwBKDflSlg9d38GX1xrwpnwcbWCzdVj-A");
north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
try {
HttpResponse response = client.execute(post);
String jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
jsonArray = new JSONArray(jsonResult);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView list = (ListView) findViewById(R.id.listView1);
List_Adapter la = new List_Adapter();
list.setAdapter(la);
}
class List_Adapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return jsonArray.length();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int index, View v, ViewGroup arg2) {
// TODO Auto-generated method stub
v = getLayoutInflater().inflate(R.layout.custom, null);
TextView tv = (TextView) v.findViewById(R.id.textView1);
try {
tv.append("id : "
+ jsonArray.getJSONObject(index).getString("id")
.toString() + "\n");
tv.append("level: "
+ jsonArray.getJSONObject(index).getString("level")
.toString() + "\n");
tv.append("time_in_secs: "
+ jsonArray.getJSONObject(index)
.getString("time_in_secs").toString() + "\n");
tv.append("par: "
+ jsonArray.getJSONObject(index).getString("par")
.toString() + "\n");
tv.append("initials: "
+ jsonArray.getJSONObject(index).getString("initials")
.toString() + "\n");
tv.append("quote: "
+ jsonArray.getJSONObject(index).getString("quote")
.toString() + "\n");
tv.append("time_stamp: "
+ jsonArray.getJSONObject(index)
.getString("time_stamp").toString() + "\n");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return v;
}
}
private StringBuilder inputStreamToString(InputStream is) {
// TODO Auto-generated method stub
String rLine = "";
StringBuilder sb = new StringBuilder();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
try {
while ((rLine = br.readLine()) != null) {
sb.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb;
}
}