Rom*_*zzi 6 google-maps google-places-api google-places android-googleapiclient
我正在使用Android的Places.GeoDataApi,我会根据执行请求的设备的位置获得不同的搜索结果.我需要将结果始终位于边界内.我没有看到在getAutocompletePredictions请求中可以设置的位置.有什么我想念的吗?
我通过以下方式使用GoogleApiClient和Places API获取地址/地点自动填充建议:
Places.GeoDataApi.getAutocompletePredictions()
Run Code Online (Sandbox Code Playgroud)
该方法需要一个GoogleApiClient对象,一个String来自动完成,一个LatLngBounds对象来限制搜索范围.这就是我的用法:
LatLngBounds bounds = new LatLngBounds(new LatLng(38.46572222050097, -107.75668023304138),new LatLng(39.913037779499035, -105.88929176695862));
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi.getAutocompletePredictions(googleApiClient, "Starbucks", bounds, null);
Run Code Online (Sandbox Code Playgroud)
正在使用的版本:com.google.android.gms:play-services-location:8.3.0
文档:https: //developers.google.com/places/android/autocomplete
好消息.截至2018年4月,谷歌增加了指定如何处理自动完成预测中的边界的可能性.现在您可以使用 带参数getAutocompletePredictions()的GeoDataClient类方法boundsMode
public Task<AutocompletePredictionBufferResponse> getAutocompletePredictions (String query, LatLngBounds bounds, int boundsMode, AutocompleteFilter filter)
boundsMode - 如何处理bounds参数.设置为STRICT时,提供的边界包含预测.如果设置为BIAS,则预测偏向于提供的边界.如果bounds为null,则此参数无效.
来源:https://developers.google.com/android/reference/com/google/android/gms/location/places/GeoDataClient
您可以将代码修改为类似于:
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);
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!
我遇到了同样的问题。
不幸的是,无法使用 Google Places API Android获取特定范围内的地点。
不过,您仍然可以使用 Google Places API Web Service进行附近搜索。
此处的文档: https ://developers.google.com/places/web-service/search?hl=fr
然后,您应该能够设置参数中的界限,并从 JSON 响应中获取界限 内的位置,如本答案中所述:https: //stackoverflow.com/a/32404701/5446285