gpt4 book ai didi

java - Google Places 自动完成 API 2019

转载 作者:行者123 更新时间:2023-12-02 04:38:11 26 4
gpt4 key购买 nike

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

}

关于java - Google Places 自动完成 API 2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com