地方自动填充怎么做的权利
问题描述:
我用下面的代码地方自动填充怎么做的权利
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
和
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.i(TAG, "Place: " + place.getName());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
,但没有看到anythink我想要的东西,所以我想请问该怎么办某种监听在我的EditText至极使用PlaceAutocomplete搜索位置,它应该看起来像我的EditText下是我的地图,当我把ķ它会显示所有的位置我的EditText下开始在K和我可以选择它,并用相机平稳移动标记位置
答
那么它可以与
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.zzih(searchString)
.build(this);
通知zzih
方法来完成,它可以让你通过搜索字符串来PlaceAutocomplete
。同样在不同版本的Google服务中,它可以有另一个名称。
问题是,PlaceAutocomplete
覆盖整个屏幕,所以你不能添加你的EditText
覆盖它。
当我面临同样的问题,我有我的自我实现的用户界面和使用谷歌的Web地方API,因为有些功能不存在于谷歌Android的地方API。您可以尝试使用GeoDataApi.getAutocompletePredictions()。
对于使用GeoDataApi.getAutocompletePredictions()
你应该:
-
在
Fragment
/Activity
private GoogleApiClient mGoogleApiClient;
-
实例并管理其生命周期的创建领域
@Override protected void onCreate(Bundle savedInstanceState) { mGoogleApiClient = new GoogleApiClient .Builder(this) .enableAutoManage(this, 0, this) .addApi(Places.GEO_DATA_API) .addApi(Places.PLACE_DETECTION_API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } @Override protected void onStart() { super.onStart(); if(mGoogleApiClient != null) mGoogleApiClient.connect(); } @Override protected void onStop() { if(mGoogleApiClient != null && mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } super.onStop(); }
-
创建过滤器,列表的av ailable过滤器是here
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder() .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS) .build();
-
设置界限。请注意,第一个坐标是西南,秒是东北。
LatLngBounds bounds = new LatLngBounds(new LatLng(39.906374, -105.122337), new LatLng(39.949552, -105.068779));
-
搜索自动完成预测
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, "my street", SharedInstances.session().getCity().getBounds(), typeFilter).setResultCallback(new ResultCallback<AutocompletePredictionBuffer>() { @Override public void onResult(@NonNull AutocompletePredictionBuffer buffer) { if(buffer == null) return; if(buffer.getStatus().isSuccess()) { for(AutocompletePrediction prediction : buffer) { Log.d(TAG,"Prediction placeId "+prediction.getPlaceId()); Log.d(TAG,"Prediction Primary Text "+prediction.getPrimaryText(null)); Log.d(TAG,"Prediction Secondary Text "+prediction.getSecondaryText(null)); } //Prevent memory leak by releasing buffer buffer.release(); } });
-
注意
AutocompletePrediction
不包含有关坐标的任何信息。所以如果你需要它,你必须通过placeId请求Place对象。Places.GeoDataApi.getPlaceById(mGoogleApiClient, googlePlaceId).setResultCallback(new ResultCallback<PlaceBuffer>() { @Override public void onResult(PlaceBuffer places) { if(places.getStatus().isSuccess()) { Place place = places.get(0); } //Release the PlaceBuffer to prevent a memory leak places.release(); }});
我想第3和第4是没有必要的,所以你可以空票代替。
你能告诉我一些如何在代码中使用GeoDataApi的示例吗? –
@KubaDojutrek是的。更新后 – DEADMC