点击自动填充项目的地图上的绘图标记

时间:2017-01-13 22:24:44

标签: android google-maps firebase-realtime-database autocompletetextview

我可以显示自动填充建议中的所有项目,但是当我单击某个项目时,我希望与该项目关联的经度和纬度坐标在地图上绘制。我目前不知道如何实现。 “请在这里帮忙。

我已粘贴我的方法

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) {

        }
    });
}

2 个答案:

答案 0 :(得分:0)

您应该将final ArrayAdapter<String> autoComplete更新为类似final ArrayAdapter<Place> autoComplete的内容,然后执行以下操作:

公共课地点{

private float latitude;
private float longitude;
private String name;

// implement your constructor
// implement your accessors

}

然后从您的onClick收到Place,您就可以执行place.getLatitude()place.getLongitude之类的操作。

我就是这样实现的。

答案 1 :(得分:0)

您只需要监听点击事件,并在点击某个项目时添加Marker

示例:

tv.setOnItemClickListener((adapterView, view, pos, id) -> {
    mGoogleMap.addMarker(new MarkerOptions()
            .position(mDataList.get(pos).getLocation()));
});

您需要确保您的Google地图已经加载了。