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

标签 android google-play-services google-places-api

我正在使用 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/

相关文章:

java - 不幸的是 "application"已在模拟器中停止。解决方案是什么?

android - 无法解析符号 'gms'。无法导入 Google Play 服务

swift - 第一行 tableview 部分覆盖

java - Android 应用程序中使用的服务器 Web 的 Google Places API KEY 请求被拒绝

java - 如何检查NoClassDefFoundError?

android - Flutter未针对1.10或更高版本进行构建

android - 从 admob 独立 SDK 切换到 Google Play 服务 SDK 时 apk 大小膨胀

android - Google Play 撤销了测试订阅

javascript - 返回我之前指定的 Google map 位置列表

android - Kotlin 1.4.20 - 构建速度较慢