gpt4 book ai didi

android - 如何实现 PlaceAutocompleteFragment 和 PlaceAutocompleteActivity 以获取地点详细信息

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:48 24 4
gpt4 key购买 nike

我正在使用 Google Place获取地点详情。 Google 提供了不同的方式来实现 Google Place API 以获取地点详细信息。不同的方式如 PlaceAutocompleteFragmentPlaceAutocompleteActivity 。如何区分这些以及如何使用 Google place API 实现获取地点详细信息。

最佳答案

首先需要API key并启用 Google Place API 以搜索和获取地点详细信息。将您的 API key 添加到您的应用程序 list 中,需要将 YOUR_API_KEY 替换为您自己的 API key :

<application>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
</application>

1) PlaceAutocompleteFragment

XML:

<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>

Java:

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

/*
* The following code example shows setting an AutocompleteFilter on a PlaceAutocompleteFragment to
* set a filter returning only results with a precise address.
*/
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
.build();
autocompleteFragment.setFilter(typeFilter);

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName());//get place details here
}

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

输出:

enter image description here

2) PlaceAutocompleteActivity

private void callPlaceAutocompleteActivityIntent() {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
//PLACE_AUTOCOMPLETE_REQUEST_CODE is integer for request code
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//autocompleteFragment.onActivityResult(requestCode, resultCode, data);
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.i(TAG, "Place:" + place.toString());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {

}
}
}

输出:

enter image description here

希望对您有所帮助。

编辑:将 requestCode == RESULT_CANCELED 更改为 resultCode == RESULT_CANCELED

关于android - 如何实现 PlaceAutocompleteFragment 和 PlaceAutocompleteActivity 以获取地点详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34416817/

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