【问题标题】:How to get opening hours of a Place with Place Autocomplete intent builder in Android?如何在 Android 中使用 Place Autocomplete Intent Builder 获取 Place 的营业时间?
【发布时间】:2018-04-22 12:02:05
【问题描述】:

目前,我正在构建一个功能来搜索具有 AutocompleteTextView 的公司。为了实现这一点,我使用了具有如下覆盖模式的 PlaceAutocomplete IntentBuilder:

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

我收到这样的地方:

@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.
        }
    }
}

但我在“地点”对象中找不到开放时间。我怎样才能找回它们?我只能找到使用 API 的其他人,但我更喜欢这个自动完成片段。

还尝试通过 API 调用稍后按地点 ID 获取地点详细信息,但似乎这只允许用于服务器密钥。如果有人在 Android 上使用发布密钥,那么它现在将是一个不错的修补程序。但是还是觉得很麻烦。

【问题讨论】:

  • 你试过thisthis吗?
  • 是的,我已经尝试过了,但这不起作用。由于获取地点详细信息的 API 调用需要一个带有 IP 过滤器的 API 密钥,这对客户端不起作用,并且适用于服务器。

标签: android google-maps google-places-api google-places googleplacesautocomplete


【解决方案1】:

请使用以下代码:

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"));

                } 
              }
    }

【讨论】:

  • 这个 asynctask 的输入是一个 json 字符串,我从哪里得到那个 json 字符串?我假设您指的是 API 调用。但作为 Android 客户端,您无权获得开放时间。你真的有这个工作吗?或者这只是这个答案的复制粘贴:stackoverflow.com/a/42731327/2917564。如您所见,我实际上是自己编辑的:P。
  • 请回答我的评论@chandrakant sharma
【解决方案2】:

查询可能无效。参数必须用 & 号分隔。

将 ?key=MYKEYHERE?reference= 更改为 ?key=MYKEYHERE&reference=。我用我的密钥对其进行了测试,并收到了“咖啡公司”的回复。

【讨论】:

  • 也不是我要找的答案,但肯定会查看查询参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多