Android Places自动完成如何检测每次建议的变化?

问题描述 投票:1回答:1

我知道谷歌是按发送的字符收费,在每次请求。这是一笔很大的开支。我需要检测每次建议数据的变化或输入文字的变化,以限制使用.在swift中,我使用这个func。

func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {

}

但是我不知道如何用安卓系统来做。我在这里找到了教程文件 https:/developers.google.compacesandroid-sdkreferencecomgoogleandroidlibrariesplaceswidgetAutocompleteSupportFragment。 但我没有看到任何关于自动完成预测的方法。

我试过这种方式,但不能

EditText inputSearch = autocompleteFragment.getView().findViewById(R.id.places_autocomplete_search_input);
    inputSearch.addTextChangedListener(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) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
});
java android google-maps google-places-api
1个回答
0
投票

谷歌的地方自动完成是按次收费的 会议而不是每个字符,只要你使用会话令牌(强烈建议使用会话令牌来降低 费用).

然后您可以使用 onPlaceSelected 如下:

// 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);
    }
});

或与 onActivityResult 使用意图。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}

或者编程。

  placesClient.findAutocompletePredictions(request).addOnSuccessListener((response) -> {
     for (AutocompletePrediction prediction : response.getAutocompletePredictions()) {
         Log.i(TAG, prediction.getPlaceId());
         Log.i(TAG, prediction.getPrimaryText(null).toString());
     }
  }).addOnFailureListener((exception) -> {
     if (exception instanceof ApiException) {
         ApiException apiException = (ApiException) exception;
         Log.e(TAG, "Place not found: " + apiException.getStatusCode());
     }
  });

参考 此处. 希望能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.