【问题标题】:AutocompleteSupportFragment not working menssage 'Unable to load search results' AndroidAutocompleteSupportFragment 不起作用消息“无法加载搜索结果”Android
【发布时间】:2020-03-16 21:24:39
【问题描述】:

我在使用 AutocompleteSupportFragment 查找地点时遇到问题,因为它没有显示任何结果并且我收到错误消息。 我已经输入了使用的新代码,因为他们要求我提供 Google 控制台密钥,但它仍然无法正常工作。 这是我的代码 我没有密码限制,不知道是不是这个问题。

Places API 已启用

谢谢。

MainActivity.java

public class MainActivity extends AppCompatActivity {


PlacesClient placesClient;
List<Place.Field> placesFields= Arrays.asList(Place.Field.ID,Place.Field.NAME,Place.Field.ADDRESS);
AutocompleteSupportFragment places_fragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initPlaces();
    setUpPlacesAutocomplete();

}


private void initPlaces() {

    Places.initialize(this,getString(R.string.places_api_key));
    placesClient=Places.createClient(this);


}

private void setUpPlacesAutocomplete() {


    places_fragment =(AutocompleteSupportFragment)getSupportFragmentManager()
            .findFragmentById(R.id.places_autocompletar);
    places_fragment.setPlaceFields(placesFields);
    places_fragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place place) {

            Toast.makeText(MainActivity.this, ""+place.getName(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(@NonNull Status status) {

            Toast.makeText(MainActivity.this, ""+status.getStatusMessage(), Toast.LENGTH_SHORT).show();

        }
    });




}

 }

activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
    android:id="@+id/places_autocompletar"
    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</fragment>


</LinearLayout>

在键的部分我有它在 NONE 选项中的应用程序限制

【问题讨论】:

  • 你是怎么解决你的问题的?

标签: google-maps android-studio google-places-api


【解决方案1】:
Please try with below code

private static final int AUTOCOMPLETE_REQUEST_CODE = 101;

 /*-- initializing Places API --**/
    if (!Places.isInitialized()) {
        Places.initialize(getActivity(), getActivity().getResources().getString(R.string.google_map_key));
    }

  /*-- function to open address search activity --**/
public void createAutoCompleteIntent() {
    if (getActivity() != null) {
        List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG, Place.Field.ADDRESS);
        Intent intent = new Autocomplete.IntentBuilder(
                AutocompleteActivityMode.FULLSCREEN, fields)
                .build(getActivity());
        startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
    }
}

 /*-- Result of Auto complete google address search --**/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            if (place.getLatLng() != null) {
                // reverse geoCoding to get Street Address, city,state and postal code

                Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
                try {
                    System.out.println("------addressList-----" + place.getAddress() + "             " + place.getName());
                    List<Address> addressList = geocoder.getFromLocation(
                            place.getLatLng().latitude, place.getLatLng().longitude, 1);
                    System.out.println("------addressList-----" + addressList);
                    if (addressList != null && addressList.size() > 0) {
                        Address address = addressList.get(0);
                        System.out.println("------address-----" + address);
                        addressEd.setText(address.getAddressLine(0));
                        String featureName = "";
                        if (address.getFeatureName()!=null){
                            featureName = address.getFeatureName();
                        }
                        String throughFare = "";
                        if (address.getThoroughfare()!=null){
                            throughFare = address.getThoroughfare();
                        }
                        String streetAddress = featureName + " " + throughFare;
                        streetAddressEd.setText(streetAddress);
                        cityEd.setText(address.getLocality());
                        stateEd.setText(address.getAdminArea());
                        postCodeEd.setText(address.getPostalCode());
                        countryEd.setText(address.getCountryName());
                    }

                } catch (IOException e) {
                    Log.e(TAG, "Unable connect to Geocoder", e);
                }

            }
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            Status status = Autocomplete.getStatusFromIntent(data);
            if (getActivity() != null) {
                Util.showMessageBar(getActivity(), status.getStatusMessage());
            }
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}

【讨论】:

  • 我试过了,仍然遇到同样的问题“无法加载搜索结果”。我已将结算帐户添加到我的项目中
  • 您是否已将此添加到清单文件中?
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多