7

I am making a android application that needs to check if the user is close to a place of interest e.g. movie theater. (Within 1 km)

However I do not need the user to see a map which I think is required to use the google places api. I have done some research but am not sure how to accomplish this on android. Any tips (including comments or relevant links would be appreciated)

  • I don't recall needing to display a map to get the results... https://developers.google.com/places/web-service/search the result is a json, you can do what you want with it. Example for nearby museum https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503186,-0.126446&radius=5000&type=museum&key=YOUR_API_KEY – Distwo Jan 24 '17 at 23:04
  • Apart from showing google maps how are you expecting user to show this data? – Sreehari Jan 31 '17 at 13:33

4 Answers4

7

to see the nearby sites,

1.- use Nearby Search Requests from google maps, link here

2.- Use phone coordinates, to get a list of movie theater near of the coords...

https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503186,-0.126446&radius=5000&types=movie+theater&key=YOUR_API_KEY

3.- compare the two coordinates, and do your magic, maybe get distance by that two points and show user the distance..

Note: if you want a lot of special places, you will need to see another way to do it. That's what I thought... is, in your server need to do a request by coords with a lot of nearest places, (museum,movie theater, etc.), and save that because google charges you when the server check in many times, when the user send the coordenates you will have a list of nearest places, if not, consult a list of best pleaces and save in your server and send to user,

you will need to use a some calculate coordenates, like range, if user is near of this coordenates that i have in database send that information if not, do request to server to consult nearest places (with your predefinite list)

Sorry for my English.

Links for more information
Find Places Nearby in Google Maps using Google Places API–Android App
Places APIs and Related Products
how to display nearby places like atm,hospitals in android google map?

Community
  • 1
  • 1
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
  • This should work well. You can get the phone location by using a broadcast receiver on the GPS , and Volley to cast the request and parse the response. – BMU Jan 25 '17 at 13:36
5

If I understood your question correctly, you are looking for GeoFences. Have a look here for more details : https://developer.android.com/training/location/geofencing.html

BMU
  • 429
  • 2
  • 9
3

Try this to get places, enter your own search query

searchString = getIntent().getStringExtra("search_key");
GetPlacesTask getPlacesTask = new GetPlacesTask();
String url = getResources().getString(R.string.near_by_search) +
            "location="+latitude+","+longitude+"&radius=1000&type=" +
            searchString +"&keyword="+ searchString +
            "&key="+ YOUR_API_KEY;
String[] stringArray = new String[]{url};
getPlacesTask.execute(stringArray);

GetPlacesTask

private class GetPlacesTask extends AsyncTask<String, String, ArrayList<Place>>
{
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected ArrayList<Place> doInBackground(String... strings)
    {
        try
        {
            URL url = new URL(strings[0]);
            HttpHandler httpHandler = new HttpHandler(url);
            String result = httpHandler.getURLResponse();
            ArrayList<Place> placesArrayList = new ArrayList<>();
            JSONObject mainResponse;
            JSONArray jsonArray;
            mainResponse = new JSONObject(jsonString);
            jsonArray = mainResponse.getJSONArray("results");
           if(jsonArray.length() > 0)
           {
               placesArrayList = fetch(jsonArray);
           }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return placesArrayList;
    }

    @Override
    protected void onPostExecute(ArrayList<Place> placesArrayList)
    {
        super.onPostExecute(placesArrayList);

        if(!placesArrayList.isEmpty())
        {
            //Show Places in your views
        }
    }
}

HttpHandler class

 public class HttpHandler
{
private URL url;
public HttpHandler(URL url)
{
    this.url = url;
}

public String getURLResponse()
{
    String response = "";

    try
    {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        InputStream is = new BufferedInputStream(urlConnection.getInputStream());
        response = convertStreamIntoString(is);
        Log.d("RESPONSE", response);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }


    return response;
}

private String convertStreamIntoString(InputStream inputStream)
{
    String response = "";
    String line;
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line);
        }
        response = sb.toString();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return response;
}
}

Here is the repository for the same

sagar
  • 379
  • 4
  • 13
2

You can also use Awareness Api for nearby places. You just have to create GoogleApiClient Object as below-

GoogleApiClient  mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Awareness.API)
                .addConnectionCallbacks(this)
                .build();

 mGoogleApiClient.connect();

Then Call below method for nearby places.

private void detectNearbyPlaces() {
        if (!checkLocationPermission()) {
            return;
        }

        Awareness.SnapshotApi.getPlaces(mGoogleApiClient)
                .setResultCallback(new ResultCallback<PlacesResult>() {
                    @Override
                    public void onResult(@NonNull PlacesResult placesResult) {
                        Place place;
                        if (placesResult.getPlaceLikelihoods() != null) {
                            for (PlaceLikelihood placeLikelihood : placesResult.getPlaceLikelihoods()) {
                                place = placeLikelihood.getPlace();
                                Log.e(TAG, place.getName().toString() + "\n" + place.getAddress().toString());
                                Log.e(TAG, "Rating: " + place.getRating());
                                Log.e(TAG, "Likelihood that the user is here: " + placeLikelihood.getLikelihood() * 100 + "%");
                            }
                        }
                    }
                });
    }

Hope it will be helpful to you.