我想用我的 SQLite 資料庫中的坐標在谷歌地圖中創建一個多邊形。我的代碼已經可以從我的資料庫中創建多個帶有坐標的標記。資料庫包含 ID、E 坐標和 N 坐標。我用 Query 嘗試了它,就像我用標記做的一樣,但它不起作用:/有人可以幫我解決這個問題嗎?(下面的代碼作業 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));
}
}
}
uj5u.com熱心網友回復:
創建多邊形只需呼叫此處描述的 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:我洗掉了相機更新,因為為每個標記/頂點移動它是沒有意義的。
注 3:請參閱https://developers.google.com/maps/documentation/android-sdk/reference/com/google/android/libraries/maps/CameraUpdateFactory#public-static-cameraupdate-newlatlngzoom-latlng-latlng,-float -zoom用于相機更新與位置和放大一個。
LatLngBounds注意 4:如果要將相機移動到中心,您可以使用此答案來獲得近似的多邊形中心: https ://stackoverflow.com/a/27601389/17856705 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/479734.html
