2015-12-22 12 views
8

Używam Google Place, aby uzyskać szczegóły miejsca. Google udostępnia inny sposób implementacji interfejsu Google Place API w celu uzyskania szczegółowych informacji o miejscu. Różne sposoby są podobne do: PlaceAutocompleteFragment, PlaceAutocompleteActivity. Jak rozróżnić te wszystkie i jak zaimplementować, aby uzyskać szczegóły miejsca za pomocą Google place API.Jak zaimplementować PlaceAutocompleteFragment i PlaceAutocompleteActivity, aby uzyskać szczegóły miejsca

Odpowiedz

28

Przede wszystkim trzeba API key i włączyć Google Place API, aby wyszukać i uzyskać szczegóły miejsca. Dodaj swój klucz API do aplikacji manifeście, konieczne zastąpienie YOUR_API_KEY z własnego klucza API:

<application> 
    ... 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="YOUR_API_KEY"/> 
</application> 

1) PlaceAutocompleteFragment

XML:

<fragment 
    android:id="@+id/place_autocomplete_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" 
    /> 

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); 
    } 
}); 

wyjściowa:

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) { 

     } 
    } 
} 

wyjściowa:

enter image description here

Nadzieję swoją pomoc.

Edycja: zmiana requestCode == RESULT_CANCELED do resultCode == RESULT_CANCELED

+0

jak można wybrać z listy autouzupełniania i pokazać je na mapie –

+0

@SrishtiRoy. Trzeba spróbować [PlaceCompleteAdapter] (https://github.com/googlesamples/android-play-places/tree/master/PlaceCompleteAdapter) – pRaNaY

+3

@pRaNaY jak dostosować wygląd PlaceAutoCompleteFragment z pobliskimi widokami w układzie –

Powiązane problemy