Cómo implementar PlaceAutocompleteFragment y PlaceAutocompleteActivity para obtener detalles del lugar

Estoy usando Google Place para obtener detalles del lugar. Google proporciona una forma diferente de implementar la API de Google Place para obtener detalles del lugar. De diferentes maneras son como PlaceAutocompleteFragment , PlaceAutocompleteActivity . Cómo diferenciarlos a todos y cómo implementarlos para obtener detalles del lugar usando Google place API.

Antes que nada necesita la clave API y Habilitar la API de Google Place para buscar y obtener detalles del lugar. Agregue su clave API al manifiesto de su aplicación, necesita reemplazar YOUR_API_KEY con su propia clave API:

 ...   

1) PlaceAutocompleteFragment

Xml:

  

Java:

 PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); /* * The following code example shows setting an AutocompleteFilter on a PlaceAutocompleteFragment to * set a filter returning only results with a precise address. */ AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS) .build(); autocompleteFragment.setFilter(typeFilter); autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { @Override public void onPlaceSelected(Place place) { // TODO: Get info about the selected place. Log.i(TAG, "Place: " + place.getName());//get place details here } @Override public void onError(Status status) { // TODO: Handle the error. Log.i(TAG, "An error occurred: " + status); } }); 

Salida:

enter image description here

2) PlaceAutocompleteActivity

 private void callPlaceAutocompleteActivityIntent() { try { Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) .build(this); startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE); //PLACE_AUTOCOMPLETE_REQUEST_CODE is integer for request code } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) { // TODO: Handle the error. } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //autocompleteFragment.onActivityResult(requestCode, resultCode, data); if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) { if (resultCode == RESULT_OK) { Place place = PlaceAutocomplete.getPlace(this, data); Log.i(TAG, "Place:" + place.toString()); } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { Status status = PlaceAutocomplete.getStatus(this, data); Log.i(TAG, status.getStatusMessage()); } else if (resultCode == RESULT_CANCELED) { } } } 

Salida:

enter image description here

Espero su ayuda.

Editar: cambiar requestCode == RESULT_CANCELED a resultCode == RESULT_CANCELED