【问题标题】:How to embed PlaceAutocompleteFragment in android app如何在 Android 应用程序中嵌入 PlaceAutocompleteFragment
【发布时间】:2016-02-24 07:08:15
【问题描述】:

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

无法解析 placeautocompletefragment 的符号

Example of what I want to show

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)

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

【问题讨论】:

  • 1) 你可以在这里发布你的代码吗?
    2)placeautocompletefragment 是否已导入到您的活动中
  • 你看看我下面的答案...
  • 检查我的答案....如果您有任何问题,请告诉我...
  • placeautocompletefragment 不会被导入。有任何库要为这个片段添加。
  • placeautocomplete 也没有导入。

标签: android google-places-api


【解决方案1】:

只需遵循以下程序,但在此之前请确保您已添加谷歌库和 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.
            }
        }
    }

【讨论】:

    【解决方案2】:

    以防万一您还没有弄清楚。然后记住:您需要在使用之前先设置 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 上查找资源时需要注意的一点是,早期的 Places 与位置服务捆绑在一起。所以互联网上的资源会这样使用它们:

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

    但是,情况不再如此。现在 Places 和 Location 是分开的。

    【讨论】:

      猜你喜欢
      • 2011-08-05
      • 2019-07-21
      • 2014-05-17
      • 1970-01-01
      • 2013-12-27
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多