Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:Places SDK,无法搜索所有位置/搜索预测有限_Android_Google Maps_Google Places Api_Google Places_Googleplacesautocomplete - Fatal编程技术网

Android:Places SDK,无法搜索所有位置/搜索预测有限

Android:Places SDK,无法搜索所有位置/搜索预测有限,android,google-maps,google-places-api,google-places,googleplacesautocomplete,Android,Google Maps,Google Places Api,Google Places,Googleplacesautocomplete,我已经实现了PlacesSDK,并使用materialSearchBar库以编程方式获取位置预测,如下面的代码所示 Places.initialize(MapsActivity.this, getString(R.string.google_maps_api)); placesClient = Places.createClient(this); final AutocompleteSessionToken token = AutocompleteSessionToken

我已经实现了PlacesSDK,并使用materialSearchBar库以编程方式获取位置预测,如下面的代码所示

    Places.initialize(MapsActivity.this, getString(R.string.google_maps_api));
    placesClient = Places.createClient(this);
    final AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();

    materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
        @Override
        public void onSearchStateChanged(boolean enabled) {

        }

        @Override
        public void onSearchConfirmed(CharSequence text) {
            startSearch(text.toString(), true, null, true);
        }

        @Override
        public void onButtonClicked(int buttonCode) {
            if (buttonCode == MaterialSearchBar.BUTTON_NAVIGATION) {
                if(drawerLayout.isDrawerOpen(GravityCompat.START)){

                }else{
                    drawerLayout.openDrawer(GravityCompat.START);
                }
            }else if (buttonCode == MaterialSearchBar.BUTTON_BACK) {
                materialSearchBar.disableSearch();
            }

        }
    });

    materialSearchBar.addTextChangeListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            RectangularBounds bounds = RectangularBounds.newInstance(
                    new LatLng(24.856165, 66.669119),
                    new LatLng(25.125024, 67.899587));
            FindAutocompletePredictionsRequest predictionsRequest = FindAutocompletePredictionsRequest.builder()
                    .setCountry("PK")
                    .setTypeFilter(TypeFilter.ADDRESS)
                    .setLocationBias(bounds)
                    .setSessionToken(token)
                    .setQuery(s.toString())
                    .build();
            placesClient.findAutocompletePredictions(predictionsRequest).addOnCompleteListener(new OnCompleteListener<FindAutocompletePredictionsResponse>() {
                @Override
                public void onComplete(@NonNull Task<FindAutocompletePredictionsResponse> task) {
                    if (task.isSuccessful()) {
                        FindAutocompletePredictionsResponse predictionsResponse = task.getResult();
                        if (predictionsResponse != null) {
                            predictionList = predictionsResponse.getAutocompletePredictions();
                            List<String> suggestionsList = new ArrayList<>();
                            for (int i = 0; i < predictionList.size(); i++) {
                                AutocompletePrediction prediction = predictionList.get(i);
                                suggestionsList.add(prediction.getFullText(null).toString());
                            }
                            materialSearchBar.updateLastSuggestions(suggestionsList);
                            if (!materialSearchBar.isSuggestionsVisible()) {
                                materialSearchBar.showSuggestionsList();
                            }
                        }
                    } else {
                        Log.i("mytag", "prediction fetching task unsuccessful");
                    }
                }
            });
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


    materialSearchBar.setSuggestionsClickListener(new SuggestionsAdapter.OnItemViewClickListener() {
        @Override
        public void OnItemClickListener(int position, View v) {
            if (position >= predictionList.size()) {
                return;
            }
            AutocompletePrediction selectedPrediction = predictionList.get(position);
            String suggestion = materialSearchBar.getLastSuggestions().get(position).toString();
            materialSearchBar.setText(suggestion);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    materialSearchBar.clearSuggestions();
                }
            }, 1000);
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            if (imm != null)
                imm.hideSoftInputFromWindow(materialSearchBar.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
            final String placeId = selectedPrediction.getPlaceId();
            List<Place.Field> placeFields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.NAME, Place.Field.ADDRESS,Place.Field.ADDRESS_COMPONENTS);

            FetchPlaceRequest fetchPlaceRequest = FetchPlaceRequest.builder(placeId, placeFields).build();
           // FetchPlaceRequest fetchPlaceRequest = FetchPlaceRequest.newInstance(placeId, placeFields);
            placesClient.fetchPlace(fetchPlaceRequest).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {
                @Override
                public void onSuccess(FetchPlaceResponse fetchPlaceResponse) {
                    Place place = fetchPlaceResponse.getPlace();
                    Log.i("mytag", "Place found: " + place.getName());
                    Log.i("mytag", "Place found: " + place.getAddress());



                    //FETCH SELECTED PLACE LAT LNG
                    latLngOfPlace = place.getLatLng();
                    Log.i("mytag", "LAT: " + latLngOfPlace.latitude);
                    Log.i("mytag", "LNG: " + latLngOfPlace.longitude);
                    //ANIMATE CAMERA TO SELECTED LOCATION
                    if (latLngOfPlace != null) {
                       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLngOfPlace, DEFAULT_ZOOM));
                    }
                   else{
                        Toast.makeText(MapsActivity.this, "unable to get location", Toast.LENGTH_SHORT).show();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if (e instanceof ApiException) {
                        ApiException apiException = (ApiException) e;
                        apiException.printStackTrace();
                        int statusCode = apiException.getStatusCode();
                        Log.i("mytag", "place not found: " + e.getMessage());
                        Log.i("mytag", "status code: " + statusCode);
                    }
                }
            });
        }

        @Override
        public void OnItemDeleteListener(int position, View v) {

        }
    });

我不明白在以编程方式实现它时我做错了什么。非常感谢你的帮助。提前感谢。

编程请求的两个参数可能会导致意外结果:

  • .setTypeFilter(TypeFilter.ADDRESS)
    只返回地址,例如用户是否要键入传递地址。如果您希望搜索栏用于搜索像麦当劳这样的企业,您需要使用
    .setTypeFilter(TypeFilter.restitution)
    筛选类型建立。如果不确定用户将使用哪种查询,请不要设置类型筛选器,因为这是一个可选参数

  • .setLocationBias(bounds)
    您选择的矩形边界的角看起来相当宽(经度相距很远,约77公里)和短(纬度相对较近,约27公里)。仔细检查这些边界是否真实反映了要限制搜索的西南和东北边界

  • 这是供参考的文件

      //INITIALIZE PLACES API
        Places.initialize(MapsActivity.this, getString(R.string.google_maps_api));
        placesClient = Places.createClient(this);
    
        // Initialize the AutocompleteSupportFragment.
        AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    
        // Specify the types of place data to return.
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
    
        // Set up a PlaceSelectionListener to handle the response.
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.i("tag", "Place: " + place.getName() + ", " + place.getId());
            }
    
            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.i("tag", "An error occurred: " + status);
            }
        });