使用 SQLite 数据库 Android 中的坐标在 Google 地图中创建多边形

Create a Polygon in Google Maps with Coordinates from a SQLite Database Android

我想在 Google 地图中用我的 SQLite 数据库中的坐标创建一个多边形。我的代码已经可以从我的数据库中创建多个带有坐标的标记。数据库包含 ID、E 坐标和 N 坐标。我用一个查询试了一下,就像我用标记做的一样,但它不起作用:/ 有人可以帮我解决这个问题吗? (以下代码 100% 有效) 感谢您的帮助!

数据库:

// This is only a part of the database
public class DBHelper extends SQLiteOpenHelper {
    public static final String KEY_ID = "key_id";
    public static final String KEY_E_COORDINATES = "e_coordinates";
    public static final String KEY_N_COORDINATES = "n_coordinates";
    public DBHelper(Context context) {super(context, DATABASE_NAME, null,DATABASE_VERSION);}

    @Override
    public void onCreate(SQLiteDatabase db) {
           db.execSQL("create table " + TABLE_FINDS + "("
                       + KEY_ID + " text, "
                       + KEY_E_COORDINATES + " text, "
                       + KEY_N_COORDINATES + " text, "
                       + ")");

地图活动:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    ArrayList<String[]> IDs = new ArrayList<String[]>();
    DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        dbHelper = new DBHelper(mapFragment.getContext());
        String[] columns = {DBHelper.KEY_AREA};

        SQLiteDatabase database = dbHelper.getWritableDatabase();                  //SQLiteDB
        ContentValues contentValues = new ContentValues();
        String selectQuery = "SELECT key_id,e_coordinates,n_coordinates FROM  finds ";
        // Select the Coordinates with IDs from the Table and save it in tmp
        Cursor cursor = database.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                String[] tmp = new String[3];
                tmp[0] = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.KEY_E_COORDINATES));
                tmp[1] = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.KEY_N_COORDINATES));
                tmp[2] = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.KEY_ID));
                IDs.add(tmp);

            }
            while (cursor.moveToNext());
        } else
            Log.d("mLog", "0 rows");
        cursor.close();

    }
@Override
    public void onMapReady(GoogleMap googleMap) {
            // Place the Markers on the Map
            for (String[] pos : IDs) {
            LatLng tmp = new LatLng(Double.parseDouble(pos[0]), Double.parseDouble(pos[1]));
            googleMap.addMarker(new MarkerOptions().position(tmp).title("ID:" + pos[2]).icon(BitmapDescriptorFactory.defaultMarker(new Random().nextInt(360))));
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(tmp));
            googleMap.moveCamera(CameraUpdateFactory.zoomTo(17.0f));
        }
    }
}

创建多边形只需调用 API,如下所述:https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/model/Polygon

在你的情况下,你几乎就在那里 - 修改你的 onMapReady - 如果你愿意,你可以留下标记(它们将位于顶点):

@Override
public void onMapReady(GoogleMap googleMap) {

        PolygonOptions po = new PolygonOptions();

        // Place the Markers on the Map
        for (String[] pos : IDs) {
            LatLng tmp = new LatLng(Double.parseDouble(pos[0]), Double.parseDouble(pos[1]));
            googleMap.addMarker(new MarkerOptions().position(tmp).title("ID:" + pos[2]).icon(BitmapDescriptorFactory.defaultMarker(new Random().nextInt(360))));
            po.add(tmp);
        }
        // set some polygon attributes for display
        po.strokeColor(Color.RED);
        po.fillColor(Color.BLUE);
        Polygon myFirstPolygon = googleMap.addPolygon(po);

        // here you can add back the camera update - just one!
        // also see the "newLatLngZoom" for camera update 


}

}

注意 1:您应该在调用 mapFragment.getMapAsync(this); 之前完成数据库读取操作,否则可能会出现竞争条件。 (只需将其移动到 onCreate 的末尾即可)。

注意 2:我删除了相机更新,因为为每个 marker/vertice 移动它没有意义。

注意 3:请参阅 https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/CameraUpdateFactory#public-static-cameraupdate-newlatlngzoom-latlng-latlng,-float-zoom 以了解具有位置和放大功能的相机更新。

注意 4:如果您想将相机移动到中心,您可以使用此答案通过 LatLngBounds 获得近似的多边形中心: .