[英]Google Places Autocomplete Api 2019
I have been trying for days now and I just can't find a solution for using the Google's places autocomplete API. 我已经尝试了几天,但我找不到使用Google的地方自动填充API的解决方案。 All the methods that I found were majority of the older version. 我发现的所有方法都是旧版本的大多数。 Now, with the Google Play Services version of the Places SDK for Android (in Google Play Services 16.0.0) being deprecated, I can't seem to find a working updated code. 现在,Google Play服务版的Places SDK for Android(在Google Play Services 16.0.0中)已被弃用,我似乎无法找到有效的更新代码。 All the solutions I find online are not working for me and I can't even understand what is happening. 我在网上找到的所有解决方案都不适合我,我甚至无法理解发生了什么。 Can someone just elaborate the code for the new way to use the Google Place Autocomplete API. 有人可以详细说明使用Google Place Autocomplete API的新方式的代码。
This is what I have done till now. 这是我到目前为止所做的。
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);
}
});
}
}
Layout File 布局文件
<?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>
When I run the above code, the search bars comes with the google watermark, but just as I type in something, the bar just closes. 当我运行上面的代码时,搜索栏带有谷歌水印,但就像我输入的东西一样,栏就关闭了。 I have no idea why. 我不知道为什么。
Just call this function in Oncreate(); 只需在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();
}
Search Bar close reason is this error 搜索栏关闭原因是此错误
Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null} 状态{statusCode = PLACES_API_ACCESS_NOT_CONFIGURED,resolution = null}
Follow this tapes 请关注此录音带
Add the Gradle Dependancy 添加Gradle Dependancy
implementation 'com.google.android.libraries.places:places:1.0.0'
Initialize the Place API 初始化Place API
Places.initialize(getApplicationContext(), "YOUR_API_KEY");
Start the Autocomplete Intent 启动自动完成意图
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields)
.build(this);
startActivityForResult(intent, 1101);
onActivityResult onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == request_code) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Log.i(TAG, "ManishPlace: " + place.getName() + ", " + place.getId());
txt_search.setText(place.getName());
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
Or use this GitHub example created by me Master-Google-Place-API 或者使用我使用Master-Google-Place-API创建的这个GitHub示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.