AutocompleteSupportFragment not returning results

浪子不回头ぞ 提交于 2021-02-11 08:45:42

问题


Bellow is my code for your reference... I followed this from some example provided in a blog..

    SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);   

    try {

        Places.initialize(mContext, getString(R.string.google_maps_key));
        PlacesClient placesClient = Places.createClient(mContext);

        // Initialize the AutocompleteSupportFragment.
        AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) this.getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG));
        autocompleteFragment.setCountry("IN");
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                AppLogger.getInstance().e(TAG, "Place: " + place.getName() + ", " + place.getId());
                AppLogger.getInstance().e(TAG, "Lat Long: " + place.getLatLng().latitude + ", " + place.getLatLng().longitude);

                if (mCurrLocationMarker != null) {
                    LatLng locLatLng = place.getLatLng();

                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(locLatLng);
                    markerOptions.title("Current Location");
                    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
                    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locLatLng, 20));

                    updateDistance(locLatLng);

                    et_address.setText(Globals.getAddress(mContext, locLatLng.latitude, locLatLng.longitude));
                }
            }

            @Override
            public void onError(Status status) {
                AppLogger.getInstance().e(TAG, "An error occurred: " + status);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        AppLogger.getInstance().e(TAG, "Exception: " + e.getLocalizedMessage());
    }

Triggeing by below code

    List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG);

    Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields)
            .setTypeFilter(TypeFilter.ADDRESS)
            .build(mContext);
    startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

Some times it gives perfect results... But most case it is giving error like below...

Thanks in advance

来源:https://stackoverflow.com/questions/59720428/autocompletesupportfragment-not-returning-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!