gpt4 book ai didi

android - 将个人位置添加到 Google Places 自动完成列表

转载 作者:行者123 更新时间:2023-11-29 23:38:09 26 4
gpt4 key购买 nike

有没有办法将地址固定到 Android 应用的位置自动完成搜索的顶部?

http://jsfiddle.net/v9rt3t2y/

This example is in JS/HTML, but I couldn't find anything for native android studio.

最佳答案

无论如何你都可以使用定制的 AutoCompleteTextView 就像在this文章。您应该在 protected void publishResults(CharSequence constraint, FilterResults results) 中添加自定义地名在填写来自 results.values; 的地点列表之后来自 Task<AutocompletePredictionBufferResponse> .

因此,对于自定义地名 "-> Custom place <-"MainActivity.java喜欢:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final String TAG = MainActivity.class.getSimpleName();
private GoogleMap mGoogleMap;
private SupportMapFragment mMapSupportedFragment;

private AutoCompleteTextView mPlaceAutoCompleteTextView;
private PlacesAdapter mPlacesAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mPlacesAdapter = new PlacesAdapter(this);
mPlaceAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.place_autocomplete);
mPlaceAutoCompleteTextView.setThreshold(1);
mPlaceAutoCompleteTextView.setAdapter(mPlacesAdapter);

mMapSupportedFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mMapSupportedFragment.getMapAsync(MainActivity.this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
}

class PlacesAdapter extends ArrayAdapter {
Context context;
List<String> placesList = new ArrayList<>();
GeoDataClient geoDataClient;
PlacesAdapter.PlacesAutoCompleteFilter filter = new PlacesAdapter.PlacesAutoCompleteFilter();

public PlacesAdapter(Context context) {
super(context, android.R.layout.simple_dropdown_item_1line,
new ArrayList<Place>());
this.context = context;

geoDataClient = Places.getGeoDataClient(context, null);
}

@Override
public int getCount() {
return placesList.size();
}

@Override
public String getItem(int position) {
return placesList.get(position);
}

@Override
public Filter getFilter() {
return filter;
}

@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {

if (view == null) {
view = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_dropdown_item_1line,
parent, false);
}

TextView textOne = view.findViewById(android.R.id.text1);
textOne.setText(placesList.get(position));

return view;
}

class PlacesAutoCompleteFilter extends Filter {
private Object lock = new Object();
private Object lockTwo = new Object();
private boolean placeResults = false;


@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
placeResults = false;
final List<String> predictedPlacesList = new ArrayList<>();

if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
results.values = new ArrayList<Place>();
results.count = 0;
}
} else {
final String searchStrLowerCase = prefix.toString().toLowerCase();

Task<AutocompletePredictionBufferResponse> task
= getAutoCompletePlaces(searchStrLowerCase);

task.addOnCompleteListener(new OnCompleteListener<AutocompletePredictionBufferResponse>() {
@Override
public void onComplete(@NonNull Task<AutocompletePredictionBufferResponse> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Auto complete prediction successful");
AutocompletePredictionBufferResponse predictions = task.getResult();
for (AutocompletePrediction prediction : predictions) {
predictedPlacesList.add((prediction.getFullText(null)).toString());
}
predictions.release();
} else {
Log.d(TAG, "Auto complete prediction unsuccessful");
}

placeResults = true;
synchronized (lockTwo) {
lockTwo.notifyAll();
}
}
});

while (!placeResults) {
synchronized (lockTwo) {
try {
lockTwo.wait();
} catch (InterruptedException e) {

}
}
}
results.values = predictedPlacesList;
results.count = predictedPlacesList.size();
Log.d(TAG, "Autocomplete predictions size after wait" + results.count);
}

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
placesList = (ArrayList<String>) results.values;
} else {
placesList = null;
}

// add your custom place here:
placesList.add(0, "-> Custom place <-");

if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}

private Task<AutocompletePredictionBufferResponse> getAutoCompletePlaces(String query) {
AutocompleteFilter.Builder filterBuilder = new AutocompleteFilter.Builder();

Task<AutocompletePredictionBufferResponse> results =
geoDataClient.getAutocompletePredictions(query, null,
filterBuilder.build());
return results;
}
}
}

}

activity_main.xml (注意!根据 Usage Limits :

  • If your app uses the autocomplete service programmatically, your UI
    must either display a 'Powered by Google' attribution, or appear
    within a Google-branded map.

) 喜欢:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="<YOUR_PACKAGE_NAME>.MainActivity">

<fragment class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

<AutoCompleteTextView android:id="@+id/place_autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:layout_margin="16dp"
android:padding="8dp"
android:inputType="text"
android:imeOptions="actionNext"
android:textSize="16sp"
android:hint="Type place name here" />
</RelativeLayout>

当您在文本字段中输入字母“a”时,您应该得到类似的内容:

Custom Place Autocomplete Filter

不要忘记在 Google Cloud Platform Console 上激活 Places SDK .

关于android - 将个人位置添加到 Google Places 自动完成列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52079206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com