2016-12-29 62 views
2

有沒有什麼方法可以使用Google Places API中的placeautocompletefragment來獲取搜索視圖中顯示的完整地址?它只顯示整個地址的第一部分。我確信一切都正確實現,並且完整地址由place.getAddress()返回。任何想法爲什麼完整的地址不顯示在搜索欄上?placeautocompletefragment完整地址

autocompleteFragment = (PlaceAutocompleteFragment) 
      getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 
    ((EditText)autocompleteFragment.getActivity().findViewById(R.id.place_autocomplete_search_input)).setTextSize(18.0f); 
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
     @Override 
     public void onPlaceSelected(Place place) { 
      // TODO: Get info about the selected place. 
      ((EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_prediction_primary_text)) 
      //autocompleteFragment.getActivity().findViewById(R.id.place_autocomplete_search_input)) 
        .setText(place.getAddress()); 
      Log.i(TAG, "Place Selected: " + place.getName() + place.getAddress()); 
     } 

     @Override 
     public void onError(Status status) { 
      // TODO: Handle the error. 
      Log.i(TAG, "An error occurred: " + status); 
     } 
    }); 
    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
      .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS) 
      .build(); 
    autocompleteFragment.setFilter(typeFilter); 

編輯: 我的應用程序功能完全一樣,在這個環節上。 https://maps-apis.googleblog.com/2015/12/autocomplete-widget-and-updated-place.html

我想要的是在搜索欄上顯示灰色文本(完整地址)而不是黑色文本(地點名稱)。有誰知道解決方案?先謝謝你!

+1

在這裏發佈您的代碼 –

回答

3

下面

public class CustomPlaceAutoCompleteFragment extends PlaceAutocompleteFragment { 
    Place place; 

    @Override 
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) { 
    this.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
     @Override public void onPlaceSelected(Place place) { 
     CustomPlaceAutoCompleteFragment.this.place = place; 
     } 

     @Override public void onError(Status status) { 

     } 
    }); 
    return super.onCreateView(layoutInflater, viewGroup, bundle); 
    } 

    @Override public void onActivityResult(int i, int i1, Intent intent) { 
    super.onActivityResult(i, i1, intent); 
    ((AppCompatEditText) getView() 
     .findViewById(R.id.place_autocomplete_search_input)).setText(place.getAddress()); 
    } 
} 

在你的XML代碼創建CustomPlaceAutoCompleteFragment

<fragment 
     android:id="@+id/place_autocomplete_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:name="com.yourpackagenaem.CustomPlaceAutoCompleteFragment" 
     /> 

您無法在onPlaceSelected中設置完整地址的原因是PlaceAutoCompleteFragment將onActivityResult中的文本名稱設置爲默認值。所以,我們需要重寫它。這就是爲什麼我建議你創建一個CustomPlaceAutoCompleteFragment。我測試了代碼。它像一個魅力。

希望它有幫助。快樂編碼:)

+0

謝謝。像魔術一樣工作 –

0

使用AutocompleteFilter得到這樣的結果完全地址:

AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() 
       .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS) 
       .build(); 

Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY).setFilter(typeFilter).build(activity); 
     startActivityForResult(intent, 1); 

源:https://developers.google.com/android/reference/com/google/android/gms/location/places/AutocompleteFilter

+0

我想我做了什麼,對吧? –

0

您不需要爲它創建自定義類,只需將它放在Main類上即可。

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     try{ 
      ((EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setText(mPlace.getAddress()); 

     }catch (Exception e){ 
       e.printStackTrace(); 
     } 

    } 
0

只需在PlaceAutocompleteFragment本身上調用setText()即可。

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
    @Override 
    public void onPlaceSelected(Place place) { 
     // TODO: Get info about the selected place. 
     autocompleteFragment.setText(place.getAddress()); 
    } 

    @Override 
    public void onError(Status status) { 
     // TODO: Handle the error. 
     Log.i(TAG, "An error occurred: " + status); 
    } 
} 
+0

這似乎不起作用。在調用onPlaceSelected之後,PlaceSelectionListener必須設置autocompleteFragment的文本 – thailey01