如何从Sqlite过滤Android Map上的标记

时间:2018-09-06 20:54:52

标签: java android google-maps checkbox maps

我想创建一个过滤器以根据用户选择显示特定标记(复选框)。

过滤器必须具有“公共标记”和“私人标记”选项,因为数据库具有“ RadioButton”列,其值将为“ public”或“ Private”。

我设法将存储在SQLite数据库中的所有标记添加/显示到我的地图中,但是我需要将它们成组排列,并允许用户选择(复选框)他们需要显示的标记。

这是我的代码,用于添加所有标记:

public void onMapReady(GoogleMap googleMap) {

mGoogleMap = googleMap;

Database_Helper dbHelper = new Database_Helper(Maps.this);
   try {
      dbHelper.getDatabaseName();
    } catch (Exception e) {
    Toast.makeText(Maps.this, "Database Does not Exists",       Toast.LENGTH_SHORT).show();
    }
  final SQLiteDatabase db = dbHelper.getReadableDatabase();

  Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        LatLngBounds.Builder mapBuilder = new LatLngBounds.Builder();
  boolean addedMarker = false;

  if (cursor != null) {
      while (cursor.moveToNext()) {

        int lat = cursor.getColumnIndexOrThrow(COL3);
        int lng = cursor.getColumnIndexOrThrow(COL4);
        int title = cursor.getColumnIndexOrThrow(COL5);
        int radiob = cursor.getColumnIndexOrThrow(COL13);

        String title = cursor.getString(title);
        double latitude = cursor.getDouble(lat);
        double longitude = cursor.getDouble(lng);
        String radio = cursor.getString(radiob);

        LatLng location = new LatLng(latitude, longitude);
        MarkerOptions options = new MarkerOptions()
                .title(event)
                .snippet(date)
                .position(location)
                .anchor(0.5F, 1.0F)
                .icon(BitmapDescriptorFactory.defaultMarker        

       (BitmapDescriptorFactory.HUE_AZURE))
                .draggable(false);

        final Marker marker = mGoogleMap.addMarker(options);
        mapBuilder.include(marker.getPosition());

        addedMarker = true;`

我的Xml布局:带有复选框

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    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="440dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    tools:context="com.sbkgroup.a411.e_connect.Maps" />

<LinearLayout
    android:layout_alignBottom="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/public1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/public1"
        android:text="Public" />

    <CheckBox
        android:id="@+id/private1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Private" />
   </LinearLayout>
 </RelativeLayout>

0 个答案:

没有答案