获取地点的开放时间-Android

获取地点的开放时间-Android,android,google-maps,google-places-api,Android,Google Maps,Google Places Api,如何在安卓系统中获得这个地方的开放时间,我有当前位置的纬度和经度 Setp-1: 我已通过调用此API“”获取位置id 此api的响应返回地址数组,从该数组将获得第一个地址位置ID 9月2日:- 获取位置id后,将此位置id传递到此API “+placeId+”&key=API_key” 问题:- 以上API未返回开放时间 请导游 感谢私人GoogleapClient MGoogleapClient; @凌驾 CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){ get

如何在安卓系统中获得这个地方的开放时间,我有当前位置的纬度和经度

Setp-1: 我已通过调用此API“”获取位置id

此api的响应返回地址数组,从该数组将获得第一个地址位置ID

9月2日:-

获取位置id后,将此位置id传递到此API “+placeId+”&key=API_key”

问题:- 以上API未返回开放时间

请导游

感谢

私人GoogleapClient MGoogleapClient;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mRootView=充气机。充气(右布局视图,容器,假);
buildGoogleAppClient();
mGoogleApiClient.connect();
Pendingreult placeResult=Places.PlaceDetectionApi.getCurrentPlace(mgoogleAppClient,null);
placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
返回mRootView;
}
/**
*创建与Google API的连接。一旦连接了API,则
*调用onConnected方法。
*/
受保护的同步无效BuildGoogleAppClient(){
mgoogleapclient=新的Googleapclient.Builder(getActivity())
.enableAutoManage(getActivity(),0,this)
.addApi(Places.PLACE\u DETECTION\u API)
.addOnConnectionFailedListener(此)
.addConnectionCallbacks(此)
.build();
}
/**
*对Places Geo Data API查询结果的回调,该查询在
*屏幕上的详细信息视图。
*/
private ResultCallback mUpdatePlaceDetailsCallback=new ResultCallback(){
@凌驾
public void onResult(PlaceLikelihoodBuffer places){
progressDialog.disclose();
如果(!places.getStatus().issucess()){
places.release();
返回;
}
placelikelike=places.get(0);
Place-Place=Place似然。getPlace();
/**
*通过地点id获取地点详细信息
*/
getPlaceOperatingHours(place.getId().toString());
places.release();
}
};
@凌驾
public void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
@凌驾
公共void onStop(){
super.onStop();
mGoogleApiClient.disconnect();
}

摘要

这是因为您实际上并没有在该位置查找业务,而是查找地址,并且地址没有开放时间

详细说明

您正在使用,它查找地址。地址没有开放时间。地址处的业务确实如此,但这些是具有不同位置ID的不同位置

您可以在链接到的示例中非常清楚地看到这一点:[注意,
sensor
是一个不推荐使用的参数,您应该忽略它]。在该响应中,结果的
类型
是像
路线
行政区_级别_3
邮政编码
等类型,显然是所有没有营业时间的实体

备选方案


由于您使用的是Android,您可能希望使用来获取当前位置,而不是反向地理编码请求。这可以返回业务。

某些位置根本没有此字段。这在逻辑上是必需的,因为他们也没有在该API的数据存储中记录小时数

您的代码应该如下所示:

String uriPath = "https://maps.googleapis.com/maps/api/place/details/json";
String uriParams = "?placeid=" + currentPlaceID + 
    "&key=" + GOOGLE_MAPS_WEB_API_KEY;
String uriString = uriPath + uriParams;
// Using Volley library for networking.
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JSONObject response = null;
// Required for the following JsonObjectRequest, but not really used here.
Map<String, String> jsonParams = new HashMap<String, String>();                
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
    uriString,
    new JSONObject(jsonParams),
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                if (response != null) {
                    // Retrieve the result (main contents).
                    JSONObject result =
                        response.getJSONObject("result");
                    // Acquire the hours of operation.
                    try {
                        JSONObject openingHoursJSON =
                            result.getJSONObject("opening_hours");
                        // Determine whether this location 
                        // is currently open.
                        boolean openNow = 
                            openingHoursJSON.getBoolean("open_now");
                        // Record this information somewhere, like this.
                        myObject.setOpenNow(openNow);
                    } catch (JSONException e) {
                        // This `Place` has no associated 
                        // hours of operation.
                        // NOTE: to record uncertainty in the open status,
                        // the variable being set here should be a Boolean 
                        // (not a boolean) to record it this way.
                        myObject.setOpenNow(null);
                    }
                }
                // There was no response from the server (response == null).
            } catch (JSONException e) {
                // This should only happen if assumptions about the returned
                // JSON structure are invalid.
                e.printStackTrace();
            }
        } // end of onResponse()
    }, // end of Response.Listener<JSONObject>()
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(LOG_TAG, "Error occurred ", error);
        }
    }); // end of new JsonObjectRequest(...)
// Add the request to the Volley request queue.
// VolleyRequestQueue is a singleton containing a Volley RequestQueue.
VolleyRequestQueue.getInstance(mActivity).addToRequestQueue(request);
字符串路径=”https://maps.googleapis.com/maps/api/place/details/json";
字符串uriParams=“?placeid=“+currentPlaceID+
“&key=“+GOOGLE\u MAPS\u WEB\u API\u key;
字符串uriString=uriPath+uriParams;
//使用截击库进行联网。
RequestFuture=RequestFuture.newFuture();
JSONObject响应=null;
//对于以下JsonObjectRequest是必需的,但此处并未真正使用。
Map jsonParams=newhashmap();
JsonObjectRequest=新的JsonObjectRequest(request.Method.POST,
uriString,
新的JSONObject(jsonParams),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
if(响应!=null){
//检索结果(主要内容)。
JSONObject结果=
response.getJSONObject(“结果”);
//获得工作时间。
试一试{
JSONObject openingHoursJSON=
结果:getJSONObject(“开放时间”);
//确定此位置是否为空
//目前正在开放。
布尔openNow=
getBoolean(“立即打开”);
//把这些信息记录在某个地方,像这样。
myObject.setOpenNow(openNow);
}捕获(JSONException e){
//这个“地方”没有关联的名称
//营业时间。
//注:为了记录打开状态下的不确定性,
//此处设置的变量应为布尔值
//(不是布尔值)以这种方式记录。
myObject.setOpenNow(空);
}
}
//服务器没有响应(响应==null)。
}捕获(JSONException e){
//只有在对返回的数据进行假设时,才会发生这种情况
//JSON结构无效。
e、 printStackTrace();
}
}//onResponse()的结尾
},//响应结束。侦听器()
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
String uriPath = "https://maps.googleapis.com/maps/api/place/details/json";
String uriParams = "?placeid=" + currentPlaceID + 
    "&key=" + GOOGLE_MAPS_WEB_API_KEY;
String uriString = uriPath + uriParams;
// Using Volley library for networking.
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JSONObject response = null;
// Required for the following JsonObjectRequest, but not really used here.
Map<String, String> jsonParams = new HashMap<String, String>();                
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
    uriString,
    new JSONObject(jsonParams),
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                if (response != null) {
                    // Retrieve the result (main contents).
                    JSONObject result =
                        response.getJSONObject("result");
                    // Acquire the hours of operation.
                    try {
                        JSONObject openingHoursJSON =
                            result.getJSONObject("opening_hours");
                        // Determine whether this location 
                        // is currently open.
                        boolean openNow = 
                            openingHoursJSON.getBoolean("open_now");
                        // Record this information somewhere, like this.
                        myObject.setOpenNow(openNow);
                    } catch (JSONException e) {
                        // This `Place` has no associated 
                        // hours of operation.
                        // NOTE: to record uncertainty in the open status,
                        // the variable being set here should be a Boolean 
                        // (not a boolean) to record it this way.
                        myObject.setOpenNow(null);
                    }
                }
                // There was no response from the server (response == null).
            } catch (JSONException e) {
                // This should only happen if assumptions about the returned
                // JSON structure are invalid.
                e.printStackTrace();
            }
        } // end of onResponse()
    }, // end of Response.Listener<JSONObject>()
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(LOG_TAG, "Error occurred ", error);
        }
    }); // end of new JsonObjectRequest(...)
// Add the request to the Volley request queue.
// VolleyRequestQueue is a singleton containing a Volley RequestQueue.
VolleyRequestQueue.getInstance(mActivity).addToRequestQueue(request);