placeautocompletefragment完整地址

时间:2016-12-29 05:30:24

标签: android google-places-api

有没有办法使用Google Places API中的placeautocompletefragment在搜索视图中显示完整地址?它只显示整个地址的第一部分。我确信一切都正确实现,并且place.getAddress()返回完整的地址。知道为什么搜索栏上没有显示完整的地址吗?

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

我想要的是在搜索栏上以灰色(完整地址)显示文本,而不是黑色文本(地点名称)。有谁知道解决方案?提前谢谢!

4 个答案:

答案 0 :(得分:4)

使用以下代码创建CustomPlaceAutoCompleteFragment

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中,

<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的原因。我测试了代码。它就像一个魅力。

希望它有所帮助。快乐的编码:)

答案 1 :(得分:0)

使用18:53:30获取结果中的完整地址,如下所示:

AutocompleteFilter

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

答案 2 :(得分: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();
        }

    }

答案 3 :(得分: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);
    }
}