获得开放时间的地方-Android

时间:2016-03-15 06:35:28

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

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

SETP-1:    我通过调用此API“http://maps.googleapis.com/maps/api/geocode/json?latlng=39.7837304,-100.4458825&sensor=true

获取了地点ID

这个api的响应返回地址数组,从这个数组中得到第一个地址ID。

九月-2: -

获取地点后,将此地方ID传递给此API 'https://maps.googleapis.com/maps/api/place/details/json?placeid= “+ placeId +” &安培;键= API_KEY'

问题: -      以上API不会返回opening_hours。

请指导。

由于

3 个答案:

答案 0 :(得分:1)

<强>摘要

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

详细说明

您正在使用查找地址的Reverse Geocoding for a Latitude/Longitude。地址没有营业时间。地址上的企业可以,但这些是具有不同地点ID的不同地方。

您可以在链接到的示例中明确地看到这一点:http://maps.googleapis.com/maps/api/geocode/json?latlng=39.7837304,-100.4458825 [请注意,types是不推荐使用的参数,您应该省略它]。在该回复中,结果的route类似administrative_area_level_3postal_codendf = results.join(generated_dates, how="outer") 等类型,显然是所有没有营业时间的实体。

<强>替代

正如您在Android上一样,您可能希望使用PlaceDetectionApi.getCurrentPlace()来获取当前位置,而不是反向地理编码请求。这可以归还企业。

答案 1 :(得分:0)

有些地方根本没有这个字段。这在逻辑上是必需的,它们也没有在此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);

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

答案 2 :(得分:-1)

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