我有一个自动补全片段搜索栏,用于用户目的地的地图,我是从地点自动补全的文档中得到的,我想保存用户最近进行的搜索,有什么方法可以做到这一点吗?任何意见或建议都将不胜感激!!
这是我的自动补全片段的代码:
`
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(), apiKey);
}
PlacesClient placesClient = Places.createClient(this);
// Initialize the AutocompleteSupportFragment.
AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
LatLng angelesCityLatLng = new LatLng(15.1445, 120.5887);
autocompleteFragment.setLocationBias(RectangularBounds.newInstance(
new LatLng(angelesCityLatLng.latitude - 0.1, angelesCityLatLng.longitude - 0.1),
new LatLng(angelesCityLatLng.latitude + 0.1, angelesCityLatLng.longitude + 0.1)
));
// Specify the types of place data to return.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
// Set up a PlaceSelectionListener to handle the response.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(@NonNull Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
// Get the LatLng of the selected place
LatLng selectedPlaceLatLng = place.getLatLng();
// Define a new CameraPosition with desired zoom level
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(selectedPlaceLatLng) // Sets the center of the map to the selected place's LatLng
.zoom(20) // Desired zoom level
.build();
// Move the camera to the new position
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// Remove the previous marker if it exists
if (markerSelectedLoc != null) {
markerSelectedLoc.remove();
}
// Create a new marker for the selected location
markerSelectedLoc = mMap.addMarker(new MarkerOptions()
.position(selectedPlaceLatLng)
.flat(true));
checkDistanceToCustomRoutes(selectedPlaceLatLng, mlocation);
}
@Override
public void onError(@NonNull Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}`