【问题标题】:Plot marker on map from autocomplete item clicked从点击的自动完成项目在地图上绘制标记
【发布时间】:2017-01-13 22:24:43
【问题描述】:

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

我在下面粘贴了我的方法

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

        }
    });
}

【问题讨论】:

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


    【解决方案1】:

    您应该将final ArrayAdapter&lt;String&gt; autoComplete 更新为final ArrayAdapter&lt;Place&gt; autoComplete 之类的东西,然后实现类似的东西:

    公共课场所{

    private float latitude;
    private float longitude;
    private String name;
    
    // implement your constructor
    // implement your accessors
    

    }

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

    我就是这样实现的。

    【讨论】:

    • 我从 Firebase 而非 Google Place API 获取数据
    【解决方案2】:

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

    例子:

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

    当然,您需要确保您的谷歌地图已加载。

    【讨论】:

    • 他们使用的是 Firebase 而不是 PlaceAutocomplete 小部件。
    • 他们是否使用 Firebase 与这个问题无关,我也没有提到 Place Autocomplete。
    • 你能解释一下我的意思是 mDataList 是从哪里来的或者如何初始化它?
    猜你喜欢
    • 2023-03-16
    • 2015-04-08
    • 2013-03-23
    • 2012-09-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多