GeoDataClient.getAutocompletePredictions - 取消/忽略以前的请求

时间:2018-06-11 14:44:59

标签: android google-places-api google-places googleplacesautocomplete

Android SDK https://developers.google.com/places/android-sdk/autocomplete (以编程方式查找获取地点预测)

GeoDataClient.getAutocompletePredictions() - 我想忽略不是最后一个请求的响应... autoComplete输入例如: '新' - >'新Y' - >'新Yo'

3回应 - 但我只想抓住最后一个...... (不使用RX)

// Submit the query to the autocomplete API and retrieve a PendingResult that will
   // contain the results when the query completes.
   Task<AutocompletePredictionBufferResponse> results =
           geoDataClient.getAutocompletePredictions(constraint, bounds, typeFilter);
   results.addOnSuccessListener(autocompletePredictions -> {
       if (autoCompletePredictionsListener != null) {
           autoCompletePredictionsListener.onAutoCompleteSuccess(autocompletePredictions);
       }
//****Here I want to ignore(or cancel somewhere before) previous requests
       autocompletePredictions.release();
   });

iOS SDK - 由Google Developers解决 https://developers.google.com/places/ios-sdk/reference/interface_g_m_s_autocomplete_fetcher

如果这些预测是针对最近一次调用sourceTextHasChanged的文本提供的,那么只会使用预测结果调用该委托。

2 个答案:

答案 0 :(得分:2)

最近有类似的需求,这就是我为达到预期效果所做的工作

// create a class-scope variable to track the most recent query
private String lastQuery;
private GeoDataClient geoDataClient;

// wrap the geoDataClient.getAutocompletePredictions in a class to associate the prediction results with the query that triggered the call
class AutocompletePredictor {
    String query;

    AutocompletePredictor(String query) {
        this.query = query;
    }

    Task<AutocompletePredictionBufferResponse> getPredictions(LatLngBounds bounds, AutocompleteFilter typeFilter) {
        return geoDataClient.getAutocompletePredictions(query, bounds, typeFilter);
    }
}

// modify your method that triggers the autocomplete filter
void filterAutocomplete(String constraint) {
    // update lastQuery every time this method is called
    lastQuery = constraint;

    // Submit the query to the autocomplete API and retrieve a PendingResult that will contain the results when the query completes.
    final AutocompletePredictor predictor = new AutocompletePredictor(constraint);
    Task<AutocompletePredictionBufferResponse> results = predictor.getPredictions(bounds, typeFilter);

    results.addOnSuccessListener(autocompletePredictions -> {
        // checks if the query for this filter is same as the most recent query issued to this method
        if (autoCompletePredictionsListener != null && predictor.query.equals(lastQuery)) {
            autoCompletePredictionsListener.onAutoCompleteSuccess(autocompletePredictions);
        }

        autocompletePredictions.release();
    });
}

编辑:当用户输入时延迟通话...
每次EditText的内容发生更改(每次用户键入字符时)都可以调用自动完成方法,而不是在实际执行之前安排自动完成调用等待一段时间。如果EditText内容在等待期过去之前再次更改,请取消之前的计划并重新计划。

editText.addTextChangedListener(new TextWatcher() {
    int delayMilliseconds = 500;
    Handler handler = new Handler();

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    @Override
    public void afterTextChanged(Editable editable) {
        final String constraint = editable.toString();

        // remove all delayed/pending tasks set in the last 500 milliseconds
        handler.removeCallbacksAndMessages(null);

        // setup a new delayed task to execute after 500 milliseconds
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                filterAutocomplete(constraint);
            }
        }, delayMilliseconds);
    }
});

答案 1 :(得分:0)

这不好,因为您要做的只是显示过滤结果,但是实际上,所有geoDataClient任务最终都是异步完成的,从而大大减少了使用配额。处理程序可以部分解决此问题。

因此,我们需要以常规API调用的方式来取消geoDataClient.getAutocompletePrediction任务。