0

I have a map fragment which I populate it with some markers that I download from an API, I have added a SearchView in top of my fragment and now I want to search through markers by title or snipped. and while typing showing a list of items matching the query. then by tapping an item remove all other markers and zoom on the tapped marker.

how can I do that?

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>
Abed Naseri
  • 512
  • 2
  • 10
  • 18

1 Answers1

0

Below is the logic which could be used to achieve.

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

You can set an adapter to your search view with all the entries which you get it from the api. Check this SO answer on how to implement search view with suggestions

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

I assume you want to tap on the search view suggestions list item, follow the steps below

1.Set an item click listener to the searchView.

2.On the onItemClick() method , get the object of the list from the selected position, which could have the location coordinates. Do below methods in your onItemClick method.

3.Clear all the markers on the map by calling map.clear()

4.Zoom the map to the selected coordinate by calling below code

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

All the best. Happy coding. :)

SripadRaj
  • 1,687
  • 2
  • 22
  • 33
  • Thank you, but the link is not exactly what I need, I'm populating an array of my custom class, and then want to search through that array. and also I am looking for an easier to understand way to do that. – Abed Naseri Jun 27 '17 at 11:56
  • @AbedNaseri I believe that the link has pretty much info on what you're trying to achieve on autocomplete feature on search view and it seems it is easy to understand well. You should be able to do it with custom class as well. :) – SripadRaj Jun 27 '17 at 12:18
  • Thanks once again, I don't really need Auto Suggestion and I don't want to search through API, I have already populated my array of objects from API, now need to search through the array only. @SripadRaj – Abed Naseri Jun 30 '17 at 09:15
  • You can pass the array to the adapter and set it to search view. – SripadRaj Jun 30 '17 at 09:31
  • thanks but I am really beginner in Android, could you please show me with some code? @SripadRaj – Abed Naseri Jun 30 '17 at 09:56