搜索自定义地图标记并在列表中显示结果 Android

search through custom map markers and show results in a list Android

我有一个地图片段,我用从 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>

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

I want to search through markers by title or snipped. and while typing showing a list of items matching the query

您可以使用从 api 获得的所有条目为您的搜索视图设置一个适配器。查看如何根据建议实施搜索视图

Tapping an item remove all other markers and zoom on the tapped marker.

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

1.Set searchView 的项目点击侦听器。

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

3.Clear 通过调用map.clear()

地图上的所有标记

4.Zoom 通过调用以下代码

将地图映射到所选坐标
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)); 

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