在列表中单击项目时隐藏RecyclerView并获取单击的地址

时间:2016-09-16 06:20:43

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

placeResult可以给我点击位置的lat和lng但是如何获得列表中显示的实际地址?谢谢你的帮助

公共类MainContentFragment extends Fragment实现了GoogleApiClient.ConnectionCallbacks,         GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {

protected GoogleApiClient mGoogleApiClient;
private static final LatLngBounds myBounds = new LatLngBounds(
        new LatLng(-0, 0), new LatLng(0, 0));

EditText mAutocompleteView;
RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private PlacesAutoCompleteAdapter mAutoCompleteAdapter;
ImageView delete;

public MainContentFragment() {
    // Required empty public constructor
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_main_content, container, false);

    buildGoogleApiClient();
    mAutocompleteView = (EditText)v.findViewById(R.id.autocomplete_tv);
    delete = (ImageView)v.findViewById(R.id.clear_text);
    mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(getActivity(),
            R.layout.searchview_adapter, mGoogleApiClient, myBounds, null);

    mRecyclerView = (RecyclerView)v.findViewById(R.id.recycleView);
    mLinearLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLinearLayoutManager);
    mRecyclerView.setAdapter(mAutoCompleteAdapter);
    delete.setOnClickListener(this);
    mAutocompleteView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
                mAutoCompleteAdapter.getFilter().filter(s.toString());
            }else if(!mGoogleApiClient.isConnected()){
                Toast.makeText(getActivity(), Constants.API_NOT_CONNECTED, Toast.LENGTH_SHORT).show();
                Log.e(Constants.PlacesTag, Constants.API_NOT_CONNECTED);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mAutoCompleteAdapter.getItem(position);
                    final String placeId = String.valueOf(item.placeId);
                    Log.i("TAG", "Autocomplete item selected: " + item.description);
                    /*
                    Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place.
                     */

                    final PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                            .getPlaceById(mGoogleApiClient, placeId);
                    placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
                        @Override
                        public void onResult(PlaceBuffer places) {
                            if(places.getCount()==1){
                                //Do the things here on Click.....
                                Toast.makeText(getActivity(),String.valueOf(places.get(0).getLatLng()),Toast.LENGTH_SHORT).show();

                            }else {
                                Toast.makeText(getActivity(),Constants.SOMETHING_WENT_WRONG,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    Log.i("TAG", "Clicked: " + item.description);
                    Log.i("TAG", "Called getPlaceById to get Place details for " + item.placeId);
                }
            })
    );

    return  v;
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .addApi(Places.GEO_DATA_API)
            .build();
}

@Override
public void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()){
        Log.v("Google API","Connecting");
        mGoogleApiClient.connect();
    }
}

@Override
public void onPause() {
    super.onPause();
    if(mGoogleApiClient.isConnected()){
        Log.v("Google API","Dis-Connecting");
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.v("Google API Callback", "Connection Done");
}

@Override
public void onConnectionSuspended(int i) {
    Log.v("Google API Callback", "Connection Suspended");
    Log.v("Code", String.valueOf(i));
}

@Override
public void onClick(View v) {
    if(v==delete){
        mAutocompleteView.setText("");
    }
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.v("Google API Callback","Connection Failed");
    Log.v("Error Code", String.valueOf(connectionResult.getErrorCode()));
    Toast.makeText(getActivity(), Constants.API_NOT_CONNECTED,Toast.LENGTH_SHORT).show();
}

}

1 个答案:

答案 0 :(得分:1)

使用以下方法从Place

中检索数据

getName() - 这个地方的名字。

getAddress() - 地点的地址,采用人类可读的格式。

getID() - 地方的文字标识符。在本页的其余部分阅读有关地点ID的更多信息。

getPhoneNumber() - 该地点的电话号码。

getWebsiteUri() - 地点网站的URI(如果已知)。这是由与该地点相关联的企业或其他实体维护的网站。如果没有网站知道,则返回null。

getLatLng() - 地点的地理位置,指定为纬度和经度坐标。

getViewport() - 作为LatLngBounds对象返回的视口,可用于在地图上显示地点。如果地点的大小未知,则可以返回null。

getLocale() - 名称和地址已本地化的区域设置。 getPlaceTypes() - 表征此地点的地点类型列表。有关可用场所类型的列表,请参阅场所界面的文档。

getPriceLevel() - 这个地方的价格水平,以整数形式返回,其值从0(最便宜)到4(最贵)。

getRating() - 地点的汇总评级,根据汇总的用户评论以浮点形式返回,其值介于1.0到5.0之间。

在此处https://developers.google.com/places/android-api/place-details

了解更多详情