如何在Android应用程序中嵌入PlaceAutocompleteFragment

时间:2016-02-24 07:08:16

标签: android google-places-api

我想在我的Android应用中实施placeautocompletefragment。我正在尝试使用this url上的说明,但它显示了:

  

无法解析placeautocompletefragment的符号

Example of what I want to show

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)

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

2 个答案:

答案 0 :(得分:1)

只需遵循以下流程,但在此之前请确保您已添加了Google库和API密钥

1.在按钮上添加波纹管功能单击

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.然后添加此功能

 @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 :(得分:1)

万一你还没想到它。请记住:在使用之前,您需要先设置Google Play服务。设置说明为here

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

compile 'com.google.android.gms:play-services:9.2.1'

您只能在案例中包含所需的部分,即“地点”。 所以它将是:

compile 'com.google.android.gms:play-services-places:9.2.1'

在Google上查找资源时要注意的一点是早期的地方信息与地理位置服务捆绑在一起。所以互联网上的资源会像这样使用它们:

compile 'com.google.android.gms:play-services-location:X.X.X'

然而,这不是个案的任何情况。现在地点和位置是分开的。