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); 
相关问题