2016-09-16 29 views
0

placeResult可以給我點擊位置的經度和緯度,但如何獲得列表中顯示的實際地址?感謝幫助隱藏RecyclerView當物品在列表中點擊並獲得點擊地址

公共類MainContentFragment擴展片段實現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(); 
} 

}

+0

沒關係,我解決了它 – Tuan

+0

那麼你應該回答你是如何解決它,這樣其他人同樣的問題,可以學習到這個崗位解釋。 – Shadow

+0

在OnResult方法中,只需通過調用settext(item.description)來設置文本來編輯文本,以查找您點擊的地址。但是,點擊後,任何人都知道如何隱藏recyclerview? – Tuan

回答

1

使用以下方法從Place檢索數據:

getName() - 該地點的名稱。

getAddress() - 地址的地址,以可讀的格式顯示。

getID() - 該地點的文本標識符。在此頁面的其餘部分詳細瞭解地點ID。

getPhoneNumber() - 該地點的電話號碼。

getWebsiteUri() - 該地點的網站的URI,如果知道的話。這是由業務或與該地點相關的其他實體維護的網站。如果不知道網站,則返回null。

getLatLng() - 地點的地理位置,指定爲經度和緯度座標。

getViewport() - 作爲LatLngBounds對象返回的視口,用於在地圖上顯示地點。如果地點的大小未知,可能會返回null。

getLocale() - 名稱和地址本地化的語言環境。 getPlaceTypes() - 表示此地點特徵的地點類型列表。有關可用地點類型的列表,請參閱Place界面的文檔。

getPriceLevel() - 此地點的價格水平,返回值爲0(最便宜)至4(最昂貴)的整數。

getRating() - 根據彙總的用戶評論,該地點的彙總評分,以浮點形式返回,值範圍爲1.0至5.0。

在這裏找到更多的細節https://developers.google.com/places/android-api/place-details