Home > Net >  How to show nearby locations from current location android studio?
How to show nearby locations from current location android studio?

Time:09-17

I have been trying to display nearby locations from my current location. But when i run it and click the button to view the nearby locations nothing appears. The first time i ran it, it displayed but when i backed out and came back in, it didnt display anything. i tried cleaning the project and other methods.

this is the method in my main class:

public void findRestaurants(View v){
        StringBuilder stringBuilder = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
        stringBuilder.append("location=" latLngCurrent.latitude   ","  latLngCurrent.longitude);
        stringBuilder.append("&radius=" 5000);
        stringBuilder.append("&keyword=" "restaurant");
        stringBuilder.append("&key=" getResources().getString(R.string.google_map_keyy));

        String url = stringBuilder.toString();

        Object dataTransfer[] = new Object[2];
        dataTransfer[0] = mMap;
        dataTransfer[1] = url;

        getNearbyPlaces getnearbyPlaces = new getNearbyPlaces();
        getnearbyPlaces.execute(dataTransfer);



    }




public class getNearbyPlaces extends AsyncTask<Object,String,String> {
   GoogleMap mMap;
   String url;
   InputStream is;
   BufferedReader  bufferedReader;
   StringBuilder stringBuilder;
   String data;
    @Override
    protected String doInBackground(Object... objects) {
        mMap = (GoogleMap)objects[0];
        url = (String)objects[1];

        try {
            URL myurl = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) myurl.openConnection();
            httpURLConnection.connect();
            is = httpURLConnection.getInputStream();
            bufferedReader = new BufferedReader(new InputStreamReader(is));

            String line = "";
            stringBuilder = new StringBuilder();

            while((line = bufferedReader.readLine() ) != null){
                stringBuilder.append(line);
            }
            data = stringBuilder.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    @Override
    protected void onPostExecute(String s) {

        try {
            JSONObject parentObject = new JSONObject(s);
            JSONArray resultsArray = parentObject.getJSONArray("results");

            for (int i = 0; i<resultsArray.length(); i  ){
                JSONObject jsonObject = resultsArray.getJSONObject(i);
                JSONObject locationObj = jsonObject.getJSONObject("geometry").getJSONObject("location");

                String latitude = locationObj.getString("lat");
                String longitude = locationObj.getString("lng");

                JSONObject nameObject = resultsArray.getJSONObject(i);
                String name_restaurant =  nameObject.getString("name");
                String vicinity = nameObject.getString("vicinity");

                LatLng latLng = new LatLng(Double.parseDouble(latitude),Double.parseDouble(longitude));

                MarkerOptions markeroptions = new MarkerOptions();
                markeroptions.title(vicinity);
                markeroptions.position(latLng);

                mMap.addMarker(markeroptions);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

it worked the first time when i lunched it. Did i do something wrong?

CodePudding user response:

hope you are doing fine. Can you replace the lines

new getNearbyPlaces().execute(dataTransfer);

instead of these 2 lines

 getNearbyPlaces getnearbyPlaces = new getNearbyPlaces();
getnearbyPlaces.execute(dataTransfer);

I am not sure whether this is going to impact much, but this is the way AsyncTask class should be called.

If this is not working, can you share the entire code of this java page, so we can look at how findRestaurant() method is called.

  • Related