How to get Places.GeoDataApi.getAutocompletePredictions ONLY inside the bounds?

前端 未结 2 479
不思量自难忘°
不思量自难忘° 2020-12-18 06:26

I am using Places.GeoDataApi for Android and I get different search results depending on the location of the device performing the request. I need the results to be consiste

相关标签:
2条回答
  • 2020-12-18 06:51

    Good news. As of April 2018 Google added possibility to specify how to treat the bounds in autocomplete predictions. Now you can use the getAutocompletePredictions() method of GeoDataClient class with boundsMode parameter

    public Task<AutocompletePredictionBufferResponse> getAutocompletePredictions (String query, LatLngBounds bounds, int boundsMode, AutocompleteFilter filter)

    boundsMode - How to treat the bounds parameter. When set to STRICT predictions are contained by the supplied bounds. If set to BIAS predictions are biased towards the supplied bounds. If bounds is null then this parameter has no effect.

    source: https://developers.google.com/android/reference/com/google/android/gms/location/places/GeoDataClient

    You can modify your code to something similar to:

    LatLngBounds bounds = new LatLngBounds(new LatLng(38.46572222050097, -107.75668023304138),new LatLng(39.913037779499035, -105.88929176695862));
    GeoDataClient mGeoDataClient = Places.getGeoDataClient(getBaseContext());;
    
    Task<AutocompletePredictionBufferResponse> results =
            mGeoDataClient.getAutocompletePredictions("Starbucks", bounds, GeoDataClient.BoundsMode.STRICT, null);
    
    try {
        Tasks.await(results, 60, TimeUnit.SECONDS);
    } catch (ExecutionException | InterruptedException | TimeoutException e) {
        e.printStackTrace();
    }
    
    try {
        AutocompletePredictionBufferResponse autocompletePredictions = results.getResult();
    
        Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
                + " predictions.");
    
        // Freeze the results immutable representation that can be stored safely.
        ArrayList<AutocompletePrediction> al = DataBufferUtils.freezeAndClose(autocompletePredictions);
    
        for (AutocompletePrediction p : al) {
            CharSequence cs = p.getFullText(new CharacterStyle() {
                @Override
                public void updateDrawState(TextPaint tp) {
    
                }
            });
            Log.i(TAG, cs.toString());
        }
    
    } catch (RuntimeExecutionException e) {
        // If the query did not complete successfully return null
        Log.e(TAG, "Error getting autocomplete prediction API call", e);
    }
    

    I hope this helps!

    0 讨论(0)
  • 2020-12-18 07:00

    I got the same problem.

    Unfortunately, there is no way to get places in specific bounds using Google Places API Android.

    However, you can still use Nearby Search using Google Places API Web Service.

    Documentation here : https://developers.google.com/places/web-service/search?hl=fr

    You should then be able to set the bounds in parameters, and get the places inside the bounds from the JSON response, as explained in this answer : https://stackoverflow.com/a/32404701/5446285

    0 讨论(0)
提交回复
热议问题