【问题标题】:Place Autocomplete how to do this right放置自动完成如何正确执行此操作
【发布时间】:2017-10-04 09:18:08
【问题描述】:

我正在使用以下代码

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

【问题讨论】:

    标签: android google-maps location


    【解决方案1】:

    这可以用

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

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

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

    当我遇到同样的问题时,我不得不自己实现 UI 并使用 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();
           }});
      

    我猜第 3. 和 4. 段不是必需的,因此您可以改为传递 null。

    【讨论】:

    • 你能告诉我一些如何在代码中使用 GeoDataApi 的示例吗?
    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多