2016-03-15 19 views
0

如何获取android的位置的开放时间,我有当前位置的经纬度。获取地点的开放时间 - 安卓

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

这个API的响应返回地址的数组,此数组将获得第一个地址位置ID拿到的地方ID。

九月-2: - : 以上API不会返回OPENING_HOURS -

越来越到位ID传递这个地方的id到这个API 'https://maps.googleapis.com/maps/api/place/details/json?placeid= “+ placeId +” &键= API_KEY'

问题之后。

请指导。

感谢

回答

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

摘要

这是因为你没有实际查找在该位置的业务(ES),你查询地址和地址不要”没有开放时间。

详细解释

您使用Reverse Geocoding for a Latitude/Longitude,其查找的地址。地址没有开放时间。地址上的企业可以做,但这些地区有不同的地点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()来获取当前位置,而不是反向地址解析请求。这可以返回企业。

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

这占了开放时间不是用于当前天的可能性。清楚的是,这是一个异步操作。它可以做成同步的,但这超出了这个答案的范围(并且通常优选异步)。