我有一个地图片段,我用从 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>
下面是可以用来实现的逻辑。
我想通过标题或剪断来搜索标记。并在键入时显示与查询匹配的项目列表
您可以使用从 api 获取的所有条目为您的搜索视图设置适配器。检查此 SO 答案,了解如何使用建议实现搜索视图
点击一个项目删除所有其他标记并放大点击的标记。
我假设您想点击搜索视图建议列表项,请按照以下步骤操作
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));
祝一切顺利。快乐编码。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句