Google Places 自动完成 Api 2019

罗汉·瓦伊什

我已经尝试了好几天了,但找不到使用 Google 地点自动完成 API 的解决方案。我发现的所有方法都是旧版本的大部分。现在,随着 Android 版 Places SDK 的 Google Play 服务版本(在 Google Play 服务 16.0.0 中)被弃用,我似乎找不到有效的更新代码。我在网上找到的所有解决方案都不适合我,我什至无法理解发生了什么。有人可以详细说明使用 Google Place Autocomplete API 的新方法的代码。

这就是我迄今为止所做的。

public class SearchAndFilter extends AppCompatActivity {
    String TAG = "placeautocomplete";
    TextView txtView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_and_filter);
        txtView = findViewById(R.id.txtView);

        // Initialize Places.
        Places.initialize(getApplicationContext(), "MY_KEY");
        // Create a new Places client instance.
        PlacesClient placesClient = Places.createClient(this);

        // Initialize the AutocompleteSupportFragment.
        AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

        // Specify the types of place data to return.
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

        // Set up a PlaceSelectionListener to handle the response.
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                txtView.setText(place.getName()+","+place.getId());
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            }

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

布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.SearchAndFilter">
    <androidx.cardview.widget.CardView
        android:id="@+id/idCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginTop="5dp"
        app:cardCornerRadius="4dp">

        <fragment android:id="@+id/autocomplete_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
            />

    </androidx.cardview.widget.CardView>

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我运行上面的代码时,搜索栏带有谷歌水印,但就在我输入内容时,搜索栏就关闭了。我不知道为什么。

希特什·库什瓦

只需在 Oncreate() 中调用此函数;

    /**
 * Sets up the autocomplete fragment to show place details when a place is selected.
 */
private void setupAutoCompleteFragment() {
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(Place.TYPE_COUNTRY).setCountry("IN")

            .build();
    autocompleteFragment.setFilter(typeFilter);

    autocompleteFragment.setHint(getResources().getString(R.string.search_stadium_court_area));
    autocompleteFragment.searchIcon.setVisibility(View.GONE);
    autocompleteFragment.input.setTextSize(10f);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {


        @Override
        public void onPlaceSelected(Place place) {
            showPlace(getString(R.string.place_autocomplete_search_hint), place);

        }

        @Override
        public void onError(Status status) {
            showResponse("An error occurred: " + status);
        }
    });
}


    private void showPlace(String source, Place place) {
    locationSelect = AppConstants.LOCATION_SELECTED_ONITEMCLICK;

    PlaceBean placeBean = new PlaceBean();


    placeBean.setAddress(String.valueOf(place.getAddress()));
    placeBean.setLatitude(String.valueOf(place.getLatLng().latitude));
    placeBean.setLongitude(String.valueOf(place.getLatLng().longitude));
    placeBean.setName((String) place.getName());
    placeBean.setCityname(cityName);

    Intent intent = new Intent();
    intent.putExtra("location", placeBean);
    setResult(RESULT_OK, intent);
    finish();

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章