【问题标题】:GeoDataClient.getAutocompletePredictions - Canceling/Ignoring Previous RequestsGeoDataClient.getAutocompletePredictions - 取消/忽略以前的请求
【发布时间】:2018-06-11 14:44:58
【问题描述】:

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

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

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 开发者解决 https://developers.google.com/places/ios-sdk/reference/interface_g_m_s_autocomplete_fetcher

只有当这些预测是针对最近一次调用 sourceTextHasChanged 时提供的文本时,才会使用预测结果调用委托。

【问题讨论】:

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


    【解决方案1】:

    最近有类似的需求,这就是我为达到预期结果所做的事情

    // 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 的内容发生变化时调用 autocomplete 方法(可能是每次用户键入字符时),不如安排 autocomplete 调用在实际执行之前等待一段时间。如果在等待时间过去之前 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);
        }
    });
    

    【讨论】:

    • 谢谢!简单又好,在输入字符之间延迟拨出电话怎么办?
    • 很高兴我能帮忙?。我已经用一种在用户打字时延迟拨出电话的方式编辑了我的答案。您可以根据需要增加或减少 delayMilliseconds 变量的值。
    【解决方案2】:

    这不好,因为你所做的只是过滤结果显示,但实际上,所有 geoDataClient 任务最终都是异步完成的,大大减少了使用配额。 Handler 部分解决了这个问题。

    所以我们需要一些其他方法来取消 geoDataClient.getAutocompletePrediction 任务,就像常规 API 调用可以做到的那样。

    【讨论】:

      猜你喜欢
      • 2015-02-23
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 2019-07-12
      • 2012-10-08
      • 2013-04-14
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多