我正在使用以下代码:
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.
}
}
}
但是没有看到我想要的任何东西,所以我想问一下如何在我的 EditText 上做一些监听器,它使用 PlaceAutocomplete 来搜索位置,它应该看起来像我的 EditText,下面是我的地图,当我放 K它会在我的 EditText 下显示从 K 开始的所有位置,我可以选择它和标记位置,相机平滑移动
好吧,它可以用
完成 Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.zzih(searchString)
.build(this);
注意
zzih
方法,它允许您将 searchString 传递给 PlaceAutocomplete
。同样在不同版本的谷歌服务中,它可以有另一个名字。
问题是
PlaceAutocomplete
覆盖entire屏幕所以你不能添加你的EditText
在它上面。
当我遇到同样的问题时,我不得不自己实现 UI 并使用 Google Places Web API,因为 Google Places 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();
}
创建过滤器,可用过滤器列表在这里
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 段不是必需的,所以您可以传递 null 代替。