1

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;
    }

}
  • 1
    Try the following link: http://stackoverflow.com/questions/2227292/how-to-get-latitude-and-longitude-of-the-mobiledevice-in-android Even though its for a different purpose, you are simply trying to get longitude and latitude of phone. The link was the first google hit and seems pretty reliable. – TheOneWhoPrograms Feb 06 '14 at 12:01

2 Answers2

0

Have a look at this guide:

https://developer.android.com/training/location/index.html

If you follow it you will receive location updates and can put it in the request string.

mace
  • 73
  • 6
0

Try this one to get the Current location details for more details Follow android developer site.There you have full document about location aware.

First Specify App Permissions in manifest to access current loaction

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Code to retrieve the Current Location

LocationManager locationManager;
double current_latitude,current_longitude;
String location_context;
Location location;
double[] find_Location() {

    location_context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)activity.getSystemService(location_context);
    List<String> providers = locationManager.getProviders(true);
    for (String provider : providers) {
        locationManager.requestLocationUpdates(provider, 1000, 0,
            new LocationListener() {

                public void onLocationChanged(Location location) {}

                public void onProviderDisabled(String provider) {

                    Toast.makeText(activity.getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); 
                }

                public void onProviderEnabled(String provider) {

                    Toast.makeText(activity.getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
                }

                public void onStatusChanged(String provider, int status,
                        Bundle extras) {}
            });
        location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            current_latitude = location.getLatitude();
            current_longitude = location.getLongitude();

        }
    }
    return new double[]{current_latitude,current_longitude};
}
Yugesh
  • 4,030
  • 9
  • 57
  • 97