放置自动填充功能如何正确执行此操作

时间:2017-10-04 09:18:08

标签: android google-maps location

我正在使用以下代码

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开始的所有位置,我可以选择它和标记位置与相机平滑移动

1 个答案:

答案 0 :(得分:0)

可以用

完成
  Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                        .zzih(searchString)
                        .build(this);

注意zzih方法,它允许您将searchString传递给PlaceAutocomplete。此外,在谷歌服务的不同版本中,它可以有另一个名称。

问题是PlaceAutocomplete覆盖整个屏幕,因此您无法在其上添加EditText

当我遇到同样的问题时,我必须自己实施用户界面并使用Google Places Web API,因为Google Places Android API中不存在某些功能。

但您可以尝试使用GeoDataApi.getAutocompletePredictions()

要使用GeoDataApi.getAutocompletePredictions(),您应该:

  1. Fragment / Activity

    中创建字段
    private GoogleApiClient mGoogleApiClient;
    
  2. 实例化并管理其生命周期

    @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();
    }
    
  3. 创建过滤器,可用过滤器列表为here

    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .build();
    
  4. 设置边界。请注意,第一个坐标是西南,秒是东北。

    LatLngBounds bounds = new LatLngBounds(new LatLng(39.906374, -105.122337), new LatLng(39.949552, -105.068779));
    
  5. 搜索自动填充预测

    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();
        }
    });
    
  6. 请注意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();
         }});
    
  7. 我猜第3和第4段不是必需的,所以你可以传递null。