首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取place -Android的开放时间

获取place -Android的开放时间
EN

Stack Overflow用户
提问于 2016-03-15 14:35:28
回答 3查看 2.3K关注 0票数 0

如何在android中获取该地方的开放时间,我有当前位置的纬度和经度。

Setp-1:我已经通过调用'http://maps.googleapis.com/maps/api/geocode/json?latlng=39.7837304,-100.4458825&sensor=true‘接口获取了place的id

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

9月2日:-

获取位置id后,将此位置id传递给'https://maps.googleapis.com/maps/api/place/details/json?placeid="+placeId+"&key=API_KEY‘接口

问题:-上面的接口没有返回opening_hours。

请指点一下。

谢谢

EN

回答 3

Stack Overflow用户

发布于 2016-03-16 07:52:31

摘要

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

详细说明

您使用的是Reverse Geocoding for a Latitude/Longitude,它会查找地址。地址没有开放时间。地址上的企业是这样做的,但它们是不同的地方,具有不同的地点ID。

你可以在你链接到的例子中很清楚地看到这一点:http://maps.googleapis.com/maps/api/geocode/json?latlng=39.7837304,-100.4458825注意,sensor是一个不推荐使用的参数,你应该省略它。在响应中,结果的typesrouteadministrative_area_level_3postal_code等类型,显然是所有没有开放时间的实体。

替代

当你在Android上时,你可能想使用PlaceDetectionApi.getCurrentPlace()来获取当前的位置,而不是反向的地理编码请求。这可以返还业务。

票数 1
EN

Stack Overflow用户

发布于 2017-07-21 01:05:21

有些地方根本就没有这个字段。这是他们在逻辑上所要求的,也没有将小时记录在此API的数据存储中。

您的代码应如下所示:

代码语言:javascript
复制
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);

这说明了当天开放时间不可用的可能性。需要明确的是,这是一个异步操作。它可以是同步的,但这超出了这个答案的范围(异步通常是首选的)。

票数 0
EN

Stack Overflow用户

发布于 2016-03-15 21:31:16

代码语言:javascript
复制
private GoogleApiClient mGoogleApiClient;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    mRootView = inflater.inflate(R.layout.view, container, false);


    buildGoogleApiClient();
    mGoogleApiClient.connect();
    PendingResult<PlaceLikelihoodBuffer> placeResult = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
    placeResult.setResultCallback(mUpdatePlaceDetailsCallback);


    return mRootView;
}


/**
 * Creates the connexion to the Google API. Once the API is connected, the
 * onConnected method is called.
 */
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .enableAutoManage(getActivity(),0, this)
            .addApi(Places.PLACE_DETECTION_API)
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .build();
}



/**
 * Callback for results from a Places Geo Data API query that shows the first place result in
 * the details view on screen.
 */
private ResultCallback<PlaceLikelihoodBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceLikelihoodBuffer>() {
    @Override
    public void onResult(PlaceLikelihoodBuffer places) {

        progressDialog.dismiss();
        if (!places.getStatus().isSuccess()) {
            places.release();
            return;
        }

        PlaceLikelihood placeLikelihood = places.get(0);
        Place place = placeLikelihood.getPlace();

        /**
         * get the place detail by the place id
         */
        getPlaceOperatingHours(place.getId().toString());

        places.release();
    }
};

@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
public void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36004222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档