【问题标题】:search through custom map markers and show results in a list Android搜索自定义地图标记并在列表中显示结果 Android
【发布时间】:2017-06-27 10:46:22
【问题描述】:

我有一个地图片段,我用从 API 下载的一些标记填充它,我在片段顶部添加了一个 SearchView,现在我想按标题或片段搜索标记。并在键入时显示与查询匹配的项目列表。然后通过点击一个项目删除所有其他标记并放大点击的标记。

我该怎么做?

fragment_map.xml

<FrameLayout 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="com.example.abed.whitelabel.Fragments.CustomersFragment">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.SearchView
        android:id="@+id/map_search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></android.support.v7.widget.SearchView>

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


    </LinearLayout>

</FrameLayout>

【问题讨论】:

    标签: android google-maps searchview


    【解决方案1】:

    下面是可以用来实现的逻辑。

    我想按标题或片段搜索标记。并在键入时显示与查询匹配的项目列表

    您可以为您的搜索视图设置一个适配器,其中包含您从 api 获取的所有条目。查看this SO answer 了解如何使用建议实现搜索视图

    点击一个项目会移除所有其他标记并放大点击的标记。

    我假设您想点击搜索视图建议列表项,请按照以下步骤操作

    1.将项目点击监听器设置为searchView

    2.在onItemClick()方法中,从选中的位置获取列表的对象,可以有位置坐标。在onItemClick 方法中执行以下方法。

    3.调用map.clear()清除地图上的所有标记

    4.通过调用以下代码将地图缩放到所选坐标

    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(new LatLng(your_location.getLatitude(), your_location.getLongitude()))      // Sets the center of the map to location user
        .zoom(17)                   // Sets the zoom
        .build();                   // Creates a CameraPosition from the builder
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
    

    一切顺利。快乐编码。 :)

    【讨论】:

    • 谢谢,但链接并不完全是我需要的,我正在填充我的自定义类的数组,然后想要搜索该数组。而且我正在寻找一种更容易理解的方法来做到这一点。
    • @AbedNaseri 我相信该链接包含有关您在搜索视图的自动完成功能上尝试实现的目标的大量信息,而且似乎很容易理解。您也应该能够使用自定义类来做到这一点。 :)
    • 再次感谢,我真的不需要自动建议,也不想通过 API 搜索,我已经从 API 填充了我的对象数组,现在只需要搜索数组. @SripadRaj
    • 您可以将数组传递给适配器并将其设置为搜索视图。
    • 谢谢,但我真的是 Android 的初学者,你能告诉我一些代码吗? @SripadRaj
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2012-02-29
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多