2017-10-04 35 views
0

我用下面的代碼地方自動填充怎麼做的權利

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和我可以選擇它,並用相機平穩移動標記位置

回答

0

那麼它可以與

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()你應該:

  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. 創建過濾器,列表的av ailable過濾器是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是沒有必要的,所以你可以空票代替。

+0

你能告訴我一些如何在代碼中使用GeoDataApi的示例嗎? –

+0

@KubaDojutrek是的。更新後 – DEADMC