0

我正在Android平臺上開發拼車應用程序(來自我的大學)。在我的Google地圖中,我有2個地方的自動完成片段(稱爲「From」和「To」)和一個騎行請求按鈕。一旦應用程序知道用戶的當前位置,它應該將地點自動完成片段之一設置爲大學的位置,並禁止對該片段進行任何更改/編輯(即,如果用戶的位置在大學,它將設置「From」自動完成片段到大學的位置,因此,禁用該片段的功能,因爲我不想讓用戶選擇從大學以外的地方選擇)。如何控制(啓用/禁用)Google Place自動完成片段編輯功能

同樣,如果他們的位置不在大學,這意味着他們需要乘車去大學。所以,第二個片段,即「To」片段應該固定在大學的位置。因此,爲了防止用戶請求搭乘大學以外的地方,應該禁用它。

我的問題是:如何禁用/啓用地方自動填充片段?

我希望我很清楚自己正在努力完成什麼。我非常感謝任何幫助或想法,我應該如何解決這個問題。

下面的函數從onConnected()以及onLocationChanged()調用:

private void handleNewLocation(Location location) { 
    Log.i(TAG, location.toString()); 

    //Place current location marker 
    LatLng currentlatLng = new LatLng(location.getLatitude(), location.getLongitude()); 

    Location university = new Location(""); 
    university.setLatitude(-33.8688); 
    university.setLongitude(151.2093); 

    float distanceInMeters = location.distanceTo(university); 
    /* 
     NOTE: this is assuming the user is rider. 
     Compute distance between current location and University, 
     if distance is >= 1000m, then set destination ("To" fragment) to University. 
     Otherwise (distance is < 1000m), set source location ("From" fragment) to University 
    */ 
    if(distanceInMeters >= 1000.0) { 
     toPlaceAutocomplete.setText("University"); 
    } else { 
     fromPlaceAutocomplete.setText("University"); 
    } 

    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(currentlatLng); 
    markerOptions.title("I am here!"); 
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)); 
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions); 

    //move map camera 
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(currentlatLng)); 
    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 
} 

裏面的if語句,當片段的文字設定,我想禁用搜索。我怎樣才能做到這一點?

回答

0

您可以做的是獲取PlaceAutocompleteFragment中包含的AppCompatEditText對象,並致電AppCompatEditText.setEnabled(false)將其禁用。

PlaceAutocompleteFragment originAutocomplete= (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.originAutocomplete); 

AppCompatEditText originEditText = (AppCompatEditText) originAutocomplete.getView().findViewById(R.id.place_autocomplete_search_input); 

originEditText.setEnabled(false); 
相關問題