当前位置:  开发笔记 > 编程语言 > 正文

如何实现PlaceAutocompleteFragment和PlaceAutocompleteActivity以获取Place详细信息

如何解决《如何实现PlaceAutocompleteFragment和PlaceAutocompleteActivity以获取Place详细信息》经验,为你挑选了1个好方法。

我正在使用Google Place来获取地点详细信息.Google提供了不同的方式来实现Google Place API以获取地点详细信息.不同的方式是PlaceAutocompleteFragment,PlaceAutocompleteActivity.如何区分这些全部以及如何使用Google Place API获取地点详细信息.



1> pRaNaY..:

首先需要API密钥和启用Google Place API来搜索和获取地点详细信息.将API密钥添加到应用清单中,需要使用自己的API密钥替换YOUR_API_KEY:


  ...
  

1)PlaceAutocompleteFragment

XML:


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);
    }
});

输出:

在此输入图像描述

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) {

        }
    }
}

输出:

在此输入图像描述

希望它的帮助.

编辑:更改requestCode == RESULT_CANCELEDresultCode == RESULT_CANCELED


不要忘记添加:`compile'c​​om.google.android.gms:play-services-places:9.4.0'`
@pRaNaY如何使用附近的布局视图自定义PlaceAutoCompleteFragment的外观
推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有