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

Time:05-23

I want to create a Polygon in Google Maps with Coordinates out of my SQLite Database. My Code can already create multiple Markers with Coordinates out of my Database. The Database contains an ID, E-Coordinates and N-Coordinates. I tried it with a Query like I did it with the markers but it wont work :/ Can somebody help me to solve this problem? (The Code below is working 100%) Thanks for the help!

Database:

// 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, "
                         ")");

MapsActivity:

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

CodePudding user response:

Creating a polygon is simply a matter of invoking the API as described here: https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/model/Polygon

In your case, you are almost there - modify your onMapReady - you can leave the markers if you wish (they will be positioned at the vertices):

@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 


}

}

NOTE 1: you should complete the database read ops before invoking the mapFragment.getMapAsync(this); as there may be a race condition otherwise. (Just move it to the end of onCreate).

NOTE 2: I removed the camera updates since it would not make sense moving it for each marker/vertice.

NOTE 3: See https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/CameraUpdateFactory#public-static-cameraupdate-newlatlngzoom-latlng-latlng,-float-zoom for a camera update with position and zoom in one.

NOTE 4: You can use this answer to get an approximate polygon center using LatLngBounds provided you want to move the camera to the center: https://stackoverflow.com/a/27601389/17856705 .

  • Related