2016-12-29 1 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

이 XML로

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

아래의 코드로 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); 

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

+0

나는 내가 한 것, 맞습니까? –

0

메인 클래스에 넣기 만하면 맞춤 클래스를 만들 필요가 없습니다.

@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

관련 문제