Android 从单击的自动完成项在地图上打印标记
我可以显示“自动完成建议”中的所有项目,但当我单击某个项目时,我希望将与该项目关联的经纬度坐标绘制在地图上。我目前不知道如何实现`请帮忙 我已将我的方法粘贴到下面Android 从单击的自动完成项在地图上打印标记,android,google-maps,firebase-realtime-database,autocompletetextview,Android,Google Maps,Firebase Realtime Database,Autocompletetextview,我可以显示“自动完成建议”中的所有项目,但当我单击某个项目时,我希望将与该项目关联的经纬度坐标绘制在地图上。我目前不知道如何实现`请帮忙 我已将我的方法粘贴到下面 private void searchAutocomplete(){ DatabaseReference database = FirebaseDatabase.getInstance().getReference(); //Create a new ArrayAdapter with your context and
private void searchAutocomplete(){
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
//Create a new ArrayAdapter with your context and the simple layout for the dropdown menu provided by Android
final ArrayAdapter<String> autoComplete = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
//Child the root before all the push() keys are found and add a ValueEventListener()
database.child("wr").child("clubs").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Basically, this says "For each DataSnapshot *Data* in dataSnapshot, do what's inside the method.
for (DataSnapshot suggestionSnapshot : dataSnapshot.getChildren()){
//Get the suggestion by childing the key of the string you want to get.
String suggestion = suggestionSnapshot.child("name1").getValue(String.class);
//Add the retrieved string to the list
autoComplete.add(suggestion);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
AutoCompleteTextView ACTV= (AutoCompleteTextView)findViewById(R.id.autocompleteView);
ACTV.setAdapter(autoComplete);
ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
private void searchAutocomplete(){
DatabaseReference database=FirebaseDatabase.getInstance().getReference();
//根据您的上下文和Android提供的下拉菜单的简单布局创建一个新的ArrayAdapter
final ArrayAdapter autoComplete=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1);
//在找到所有push()键之前对根进行子对象化,并添加ValueEventListener()
database.child(“wr”).child(“clubs”).addValueEventListener(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//基本上,这表示“对于DataSnapshot中的每个DataSnapshot*数据*,执行方法内部的操作。
对于(DataSnapshot suggestionSnapshot:DataSnapshot.getChildren()){
//通过对要获取的字符串的键进行子化来获取建议。
String suggestion=suggestionSnapshot.child(“name1”).getValue(String.class);
//将检索到的字符串添加到列表中
自动完成。添加(建议);
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
AutoCompleteTextView ACTV=(AutoCompleteTextView)findViewById(R.id.autocompleteView);
ACTV.setAdapter(自动完成);
ACTV.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
}
});
}
您应该将final ArrayAdapter autoComplete
更新为类似于final ArrayAdapter autoComplete
的内容,然后实现如下内容:
公共课场所{
private float latitude;
private float longitude;
private String name;
// implement your constructor
// implement your accessors
}
然后从onClick
您将收到Place
,您可以执行类似Place.getLatitude()
和Place.getLongitude
的操作
这就是我将如何实现的。您只需监听单击事件,并在单击项目时添加一个
标记
例如:
tv.setOnItemClickListener((adapterView, view, pos, id) -> {
mGoogleMap.addMarker(new MarkerOptions()
.position(mDataList.get(pos).getLocation()));
});
当然,你需要确保你的谷歌地图已经加载。他们使用的是Firebase而不是PlaceAutocomplete小部件。他们是否使用Firebase与问题无关,我没有提到PlaceAutocomplete。你能解释一下吗?我的意思是,mDataList是从哪里来的,或者如何初始化它?我从Fir获取数据ebase不是googleplaceapi