【发布时间】:2016-09-16 06:20:42
【问题描述】:
placeResult 可以给我点击位置的纬度和经度,但是如何获取列表中显示的实际地址?感谢帮助
公共类 MainContentFragment 扩展 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();
}
}
【问题讨论】:
-
没关系,我解决了
-
那么你应该在这篇文章中添加一个答案,解释你是如何解决它的,以便其他有同样问题的人可以向你学习。
-
在 OnResult 方法中,只需通过调用 settext(item.description) 将文本设置为 edittext 即可检索您单击的地址。但是有谁知道我点击后如何隐藏recyclerview?
标签: android android-recyclerview google-places-api