Я создаю приложение android, в котором я использую Google Map API для получения местоположения. Я установил лимит на адреса стран (Индия). Есть ли способ установить ограничение для штата (Махараштра) и города (Пуна) в нем?
Ниже приведен мой код для извлечения адресов с помощью API-ключа карты Google
List < Place.Field > placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG, Place.Field.TYPES);
FetchPlaceRequest request = null;
if (placeID != null) {
request = FetchPlaceRequest.builder(placeID, placeFields)
.build();
}
if (request != null) {
placesClient.fetchPlace(request).addOnSuccessListener(new OnSuccessListener < FetchPlaceResponse > () {
@SuppressLint("SetTextI18n")
@Override
public void onSuccess(FetchPlaceResponse task) {
localityAddressEdt.setText(task.getPlace().getName() + "\n" + task.getPlace().getAddress());
pinCode = "";
latLng = task.getPlace().getLatLng();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
Ниже приведен код моего адаптера (getAutocomplete
метод используется для ограничения страны):
public class AutoCompleteAdapter extends ArrayAdapter < AutocompletePrediction > implements Filterable {
private ArrayList < AutocompletePrediction > mResultList;
private PlacesClient placesClient;
public AutoCompleteAdapter(Context context, PlacesClient placesClient) {
super(context, android.R.layout.simple_expandable_list_item_2, android.R.id.text1);
this.placesClient = placesClient;
}
@Override
public int getCount() {
return mResultList.size();
}
@Override
public AutocompletePrediction getItem(int position) {
return mResultList.get(position);
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View row = super.getView(position, convertView, parent);
AutocompletePrediction item = getItem(position);
TextView textView1 = row.findViewById(android.R.id.text1);
TextView textView2 = row.findViewById(android.R.id.text2);
if (item != null) {
textView1.setText(item.getPrimaryText(null));
textView2.setText(item.getSecondaryText(null));
}
return row;
}
@NonNull
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults results = new FilterResults();
// We need a separate list to store the results, since
// this is run asynchronously.
ArrayList < AutocompletePrediction > filterData = new ArrayList < > ();
// Skip the autocomplete query if no constraints are given.
if (charSequence != null) {
// Query the autocomplete API for the (constraint) search string.
filterData = getAutocomplete(charSequence);
}
results.values = filterData;
if (filterData != null) {
results.count = filterData.size();
} else {
results.count = 0;
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
mResultList = (ArrayList < AutocompletePrediction > ) results.values;
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
notifyDataSetInvalidated();
}
}
@Override
public CharSequence convertResultToString(Object resultValue) {
// Override this method to display a readable result in the AutocompleteTextView
// when clicked.
if (resultValue instanceof AutocompletePrediction) {
return ((AutocompletePrediction) resultValue).getFullText(null);
} else {
return super.convertResultToString(resultValue);
}
}
};
}
private ArrayList < AutocompletePrediction > getAutocomplete(CharSequence constraint) {
final FindAutocompletePredictionsRequest.Builder requestBuilder =
FindAutocompletePredictionsRequest.builder()
.setQuery(constraint.toString())
.setCountry("IN")
.setSessionToken(AutocompleteSessionToken.newInstance());
Task < FindAutocompletePredictionsResponse > results =
placesClient.findAutocompletePredictions(requestBuilder.build());
//Wait to get results.
try {
Tasks.await(results, 60, TimeUnit.SECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
e.printStackTrace();
}
if (results.isSuccessful()) {
if (results.getResult() != null) {
return (ArrayList < AutocompletePrediction > ) results.getResult().getAutocompletePredictions();
}
return null;
} else {
return null;
}
}
}