无法按地点ID

时间:2018-06-18 07:04:53

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

我想让加油站靠近我的位置。为此,我使用了web api,它给出了id,lat,很长的地方。 现在我想得到名称,评论等地方的详细信息。

我怎样才能找到相同的东西?

我曾尝试使用侦听器但它根本不运行。

public class GetLocationsAsyncTask extends AsyncTask<String, Void, JSONObject> {

    String api;
    JSONObject jsonParams;
    Context mContext;
    private ProgressDialog loadingDialog;
    private CoordinatorLayout parentLayout;
    private GetLocationsCallBack getLocationsCallBack;
    private ArrayList<Location> locationArrayList;
    private String latitude,longitude;
    protected GeoDataClient mGeoDataClient;
    private CharSequence pumpName;

    public GetLocationsAsyncTask(Context context, GetLocationsCallBack getLocationsCallBack) {

        this.mContext = context;
        this.getLocationsCallBack = getLocationsCallBack;

    }

    public GetLocationsAsyncTask(Context context) {

        this.mContext = context;
    }

    public interface GetLocationsCallBack {
        void doPostExecute(ArrayList<Location> list);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

            loadingDialog = ProgressDialog.show(mContext, null,"WAIT");
            loadingDialog.setCancelable(false);
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        try {

            api = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=19.155148,72.867855&radius=5000&type=gas_station&keyword=bharat&key=AIzaSyCArRAX4oHdfFWrTW5dhXrO8hqV3VBQtbs";


            jsonParams = new JSONObject();

            ServerRequest request = new ServerRequest(api, jsonParams);
            return request.sendGetRequest();

        } catch (Exception ue) {
            return Excpetion2JSON.getJSON(ue);
        }
    }  //end of doInBackground

    @Override
    protected void onPostExecute(JSONObject response) {
        super.onPostExecute(response);

        try {
            mGeoDataClient = Places.getGeoDataClient(mContext, null);


            if (response.has("results")) {


                JSONArray jsonArray = response.getJSONArray("results");


                locationArrayList = new ArrayList<>();

                for (int i = 0; i < jsonArray.length(); i++) {

                    final Location location = new Location();


                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    JSONObject geometry = jsonObject.getJSONObject("geometry");

                    final String placeId = jsonObject.getString("place_id");

                    JSONObject locationObject = geometry.getJSONObject("location");

                    latitude = locationObject.getString("lat");
                    longitude = locationObject.getString("lng");

                    mGeoDataClient.getPlaceById(placeId).addOnCompleteListener(new OnCompleteListener<PlaceBufferResponse>() {
                        @Override
                        public void onComplete(@NonNull Task<PlaceBufferResponse> task) {
                            if (task.isSuccessful()) {
                                PlaceBufferResponse places = task.getResult();
                                Place myPlace = places.get(0);
                                Log.i(TAG, "Place found: " + myPlace.getName());

                                pumpName =  myPlace.getName();

                                location.setPumpName(String.valueOf(pumpName));  // this all does not run.I need to get the name of place by place Id.

                                places.release();
                            } else {
                                Log.e(TAG, "Place not found." + placeId);
                            }
                        }
                    });

                    location.setLocation(getAddress(latitude,longitude,placeId));

                    locationArrayList.add(location);
                }

                if (getLocationsCallBack != null)
                    getLocationsCallBack.doPostExecute(locationArrayList);

                if (loadingDialog.isShowing())
                    loadingDialog.dismiss();
            }

        } catch (JSONException je) {

            je.printStackTrace();
        }
    } //end of onPostExecute

    public String getAddress(String latitude, String longitude, final String placeId) {
        String address = "";
        try {

            Geocoder geocoder;
            List<Address> addresses;
            geocoder = new Geocoder(mContext, Locale.getDefault());

            addresses = geocoder.getFromLocation(Double.parseDouble(latitude), Double.parseDouble(longitude), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
            address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
            String city = addresses.get(0).getLocality();
            String state = addresses.get(0).getAdminArea();
            String country = addresses.get(0).getCountryName();
            String postalCode = addresses.get(0).getPostalCode();
            String knownName = addresses.get(0).getFeatureName();


        } catch (IOException ie) {
            ie.printStackTrace();
        }
        return address;
    }
}

另外我还有另一个api,它提供了逐个地点ID的详细信息,但是我只想在我的地方列表中显示名称,如何从地方列表中调出每个地方的详细信息api并显示在同一列表中?

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJ02Q611O25zsRVi5Xs9NKmhc&key=AIzaSyCArRAX4oHdfFWrTW5dhXrO8hqV3VBQtbs

以上是地方详情api。

请帮助,谢谢。

1 个答案:

答案 0 :(得分:-1)

根据latlng和keyword,您可以使用Google API获取地点列表。您还可以获取特定地点的详细信息,包括googl Api的评论。

以下是api找到你附近的地方: -

  

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=经纬度&安培; rankby =距离&安培;关键字= <强>气体   站&安培;键= <强> YOUR_API_KEY

获取更多信息检查链接: - https://developers.google.com/places/web-service/search
以下是api查找特定地点的详细信息,包括评论: -

  

https://maps.googleapis.com/maps/api/place/details/json?placeid= PLACE_ID &安培;键= <强> API_KEY

用于mor信息检查链接: - https://developers.google.com/places/web-service/details