获得开放时间的地方-Android

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

Setp-1:我通过调用此API'http://maps.googleapis.com/maps/api/geocode/json?latlng=39.7837304,-100.4458825&sensor=true'获得了地点ID

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

九月-2: -

获取地点ID后,将此地点ID传递给此API'https://maps.googleapis.com/maps/api/place/details/json?placeid= "+placeId+"&key = API_KEY'

问题: - 以上API不返回opening_hours .

请指导 .

谢谢

回答(3)

2 years ago

Summary

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

Detailed explanation

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

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

Alternative

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

2 years ago

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

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