How to get opening hours of a Place with Place Autocomplete intent builder in Android?
Asked Answered
E

2

10

Currently, I am building a functionality to search for a company with an AutocompleteTextView. To achieve this I use A PlaceAutocomplete IntentBuilder with the overlay mode like this:

   try {
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ESTABLISHMENT)
                .setCountry("NL")
                .build();
        Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                .setFilter(typeFilter)
                .build(activity);
        activity.startActivityForResult(intent, placeAutocompleteRequestCode);
    } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
        Log.e(TAG, "Something went wrong getting the places fragment." + e.getMessage());
    }

I receive the place like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.i(TAG, "Place: " + place.getName());
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}

But I can't find the opening times in the 'place' object. How am I able to retrieve them? I can only find others who use the API, but I prefer this autocomplete fragment.

Also tried it with an API call to get the place details by place ID later but it appears that this is only allowed for server keys. If someone has this working with a release key on Android then it would be a nice hotfix for now. But still feels cumbersome.

Ehman answered 9/11, 2017 at 13:12 Comment(2)
Have you tried this or this?Closed
Yes I've tried that, but that doesn't work. As the API call to get place details requires an API key with IP filter, which doesn't work for clients and is meant for servers.Ehman
W
0

Please use this code below :

private class ParserTask extends AsyncTask<String, Integer, HashMap<String,String>>{

        JSONObject jObject;
        @Override
        protected HashMap<String,String> doInBackground(String... jsonData) {

            HashMap<String, String> hPlaceDetails = null;
            PlaceDetailsJSONParser placeDetailsJsonParser = new PlaceDetailsJSONParser();

            try{
                // this object give JSON
                jObject = new JSONObject(jsonData[0]);

                // this object give Result
                JSONObject jresult = new JSONObject(jObject[1]);            

                // this object give opening_hours
                JSONObject jopening_hours = new JSONObject(jresult[2]);

                // this object give periods
                JSONObject jperiods = new JSONObject(jopening_hours[2]);



            }catch(Exception e){
                Log.d("Exception",e.toString());
            }
            return jperiods;
        }

        // Executed after the complete execution of doInBackground() method
        @Override
        protected void onPostExecute(JsonObject periods){
                HashMap<String,String> periods;

                // put your condition 
                for(..)
                {
                JsonObject day = new JSONObject(periods[i]);
                    periods.put("key",day.get("day"));

                } 
              }
    }
Wallasey answered 27/11, 2017 at 6:25 Comment(2)
The input of this asynctask is a json string, where do I get that json string from? I assume that you are referring to an API call. But you're not authorized to get opening times as an Android client. Have you actually got this working? Or is this just a copy paste of this answer: https://mcmap.net/q/1169391/-how-to-get-opening_hours-using-the-google-places-api-for-android. Which I've actually edited myself as you can see :P.Ehman
Please answer my comment @chandrakant sharmaEhman
C
0

probalbly query is invalid. Parameters must be separated by ampersands.

Change ?key=MYKEYHERE?reference= to ?key=MYKEYHERE&reference=. I tested it with my key and received a response for "Coffe company".

Carlinecarling answered 28/11, 2017 at 12:22 Comment(1)
Also not the answer I was looking for, but definetely will look into the query parameters.Ehman

© 2022 - 2024 — McMap. All rights reserved.