如何在安卓中获取该地点的开放时间,我有当前位置的纬度和经度。
设置-1: 我通过调用此 API "http://maps.googleapis.com/maps/api/geocode/json?latlng=39.7837304,-100.4458825&sensor=true"获得了地点 ID
这个 api 的响应返回地址数组,从这个数组中将得到第一个地址位 ID。
9月-2日:-
获取地点 ID 后,将此地点 ID 传递到此 API 中'https://maps.googleapis.com/maps/api/place/details/json?placeid="+placeId+"&key=API_KEY'
问题:- 以上 API 不返回opening_hours。
请指导。
谢谢
总结
这是因为您实际上并不是在查找该位置的商家,而是在查找地址,并且地址没有营业时间。
详细说明
您正在对纬度/经度使用反向地理编码,用于查找地址。 地址没有开放时间。 地址的商家确实如此,但这些地点是具有不同地点 ID 的不同地点。
您可以在链接到的示例中非常清楚地看到这一点:http://maps.googleapis.com/maps/api/geocode/json?latlng=39.7837304,-100.4458825 [请注意,sensor
是一个已弃用的参数,您应该省略它]。 在该响应中,结果的types
是像route
、administrative_area_level_3
、postal_code
等类型,显然所有没有开放时间的实体。
另类
当您使用 Android 时,您可能希望使用PlaceDetectionApi.getCurrentPlace()
来获取当前地点,而不是反向地理编码请求。 这可以回报企业。
某些位置根本没有此字段。这在逻辑上是必需的,因为他们也没有在此 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);
这解释了当天无法开放的可能性。需要明确的是,这是一个异步操作。它可以是同步的,但这超出了这个答案的范围(异步通常是首选)。
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();
}