首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在安卓应用程序中嵌入PlaceAutocompleteFragment

问如何在安卓应用程序中嵌入PlaceAutocompleteFragment
EN

Stack Overflow用户
提问于 2016-02-24 07:08:15
回答 2查看 1.6K关注 0票数 2

我想在我的安卓应用程序中实现placeautocompletefragment。我正在尝试使用这个url的说明,但它显示:

无法解析占位符自动完成片段的符号

我想展示的例子

代码语言:javascript
复制
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)

getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

EN

回答 2

Stack Overflow用户

发布于 2016-02-24 07:19:23

只需遵循下面的步骤,但在此之前,请确保您已经添加了google库和api键。

1.在您的按钮上添加下面的功能

代码语言:javascript
复制
private void openAutocompleteActivity() {
        try {
            // The autocomplete activity requires Google Play Services to be available. The intent
            // builder checks this and throws an exception if it is not the case.
            Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                    .build(mActivity);
            startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
        } catch (GooglePlayServicesRepairableException e) {
            // Indicates that Google Play Services is either not installed or not up to date. Prompt
            // the user to correct the issue.
            GoogleApiAvailability.getInstance().getErrorDialog(mActivity, e.getConnectionStatusCode(),
                    0 /* requestCode */).show();
        } catch (GooglePlayServicesNotAvailableException e) {
            // Indicates that Google Play Services is not available and the problem is not easily
            // resolvable.
            String message = "Google Play Services is not available: " +
                    GoogleApiAvailability.getInstance().getErrorString(e.errorCode);

            Log.e("Tag", message);
            Toast.makeText(mActivity, message, Toast.LENGTH_SHORT).show();
        }
    }

2.然后添加此功能

代码语言:javascript
复制
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_AUTOCOMPLETE) {
            if (resultCode == RESULT_OK) {
                // Get the user's selected place from the Intent.
                Place place = PlaceAutocomplete.getPlace(mActivity, data);
                Log.i("TAG", "Place Selected: " + place.getName());

                LatLng latLng = place.getLatLng();
                latitude = latLng.latitude;
                longitude = latLng.longitude;

                Log.v(ApplicationsConstants.LoginDetails.LATITUDE, "" + latitude);
                Log.v(ApplicationsConstants.LoginDetails.LONGITUDE, "" + longitude);

                tvCity.setText(place.getName());
                Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.CITY, "" + place.getName());
                Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.LATITUDE, "" + latitude);
                Utils.saveDataToPreferences(mActivity, ApplicationsConstants.LoginDetails.LONGITUDE, "" + longitude);

            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(mActivity, data);
                Log.e("TAG", "Error: Status = " + status.toString());
            } else if (resultCode == RESULT_CANCELED) {
                // Indicates that the activity closed before a selection was made. For example if
                // the user pressed the back button.
            }
        }
    }
票数 1
EN

Stack Overflow用户

发布于 2016-07-27 20:28:25

以防你还没弄明白。然后记住:在使用Google服务之前,您需要先设置它。安装指令是这里。

需要注意的一点是,您不必像这样包括整个google服务库:

代码语言:javascript
复制
compile 'com.google.android.gms:play-services:9.2.1'

您只可以包括您需要的部分,在您的情况下,它是位置。因此,将是:

代码语言:javascript
复制
compile 'com.google.android.gms:play-services-places:9.2.1'

在google上查找资源时要注意的一点是,早期的站点与位置服务捆绑在一起。所以互联网上的资源会像这样使用:

代码语言:javascript
复制
compile 'com.google.android.gms:play-services-location:X.X.X'

然而,情况已不再是这样了。现在地点和地点是分开的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35595489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档