jeudi 1 juin 2017

Filter Marker Maps Android with Checkbox

I apologize for the long post but I would filter my google maps with checkbox.

My problem is that I have a dynamic checkbox generated by Adapter with JSonArray and I have "filter area" in one framelayout, and google maps in another fragment.

So I would like filter markers put in map when I select a checkbox.

I have in my MainActivity in function onMapReady(GoogleMap googleMap) where maps is a JsonArray with Places and markerArray = HashMap<Marker, object>

int height = 150;
int width = 150;
BitmapDrawable bitmapdraw = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_marker_restaurant);
Bitmap b = bitmapdraw.getBitmap();
Bitmap restaurantMarker = Bitmap.createScaledBitmap(b, width, height, false);

for (int i = 0; i < maps.length(); i++) {
   JSONObject place = maps.getJSONObject(i);
   Double lat = place.getDouble("lat");
   Double longitude = place.getDouble("long");
   LatLng placePosition = new LatLng(lat, longitude);
   MarkerOptions marker = new MarkerOptions().position(placePosition)
                        .title(place.getString("title"))
                        .snippet(place.getString("description));
   marker.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
   marker.anchor(0.5f,0.8f);
   Marker mark = googleMap.addMarker(marker);
   mark.setTag(place);
   markerArray.put(mark, place);
}

So I have my Maps with my dynamic marker that have in tag the entire object place.

Now I would filter this marker with a "filter area" with checkbox where I select a type of filter (ex. Restaurant) and I would that marker with category != 1 (where 1 is id of category restaurant) to hide but I have undefined number of category, so I created a Adapter for ListView wich is Inside a FrameLayout:

<FrameLayout
    xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/filterArea"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="160dp">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="@dimen/filter_area"
        android:orientation="horizontal">
       <ListView
           android:id="@+id/check_box_filter_list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">
       </ListView>
   </RelativeLayout>
</FrameLayout>

And so I create a checkBox Adapter for Listview:

public class CategoriesAdapter extends BaseAdapter {
    JSONArray categories = new JSONArray();
    Context context;
    String codeLang = Locale.getDefault().getLanguage();

    public CategoriesAdapter(Context contextD, JSONArray jsonArray) {
        this.categories = jsonArray;
        this.context= contextD;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.categories_row, null);

        TextView categoryName = (TextView) convertView.findViewById(R.id.category_name);
        CheckBox cbSelect = (CheckBox) convertView.findViewById(R.id.checkbox_category);
        try {
            JSONObject category = categories.getJSONObject(position);
            categoryName.setText(category.getString("description"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
        }
}

And view for Row:

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/category_name"/>
    <CheckBox android:id="@+id/checkbox_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

So I will have x categorie, so my question is, How I can click on checkbox in adapter and filter map marker? How can I put "id" value to checkbox?




Aucun commentaire:

Enregistrer un commentaire