0
我想在地圖上繪製自定義標記。如何組織許多標記的顯示。SQlite在Google地圖上添加多個標記
public class DatabaseHelper extends SQLiteOpenHelper {
static final String TABLE = "geotable";
public DatabaseHelper(Context context) {
super(context, "myDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table geotable ("
+ "id integer primary key autoincrement,"
+ "image text,"
+ "lng text,"
+ "lat text" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE);
onCreate(db);
}
在第一堂課我用db創建表。然後把信息在這張表
case R.id.saveToBD:
cv.put("image", tex);
cv.put("lng", lng);
cv.put("lat", ltd);
break;
然後我需要顯示所有標記從數據庫映射。但我在地圖上只看到一個(最後一個)標記。我如何組織一個循環?
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
DatabaseHelper sqlHelper;
SQLiteDatabase db;
Cursor userCursor;
private GoogleMap googleMap;
private String imageTest;
double lngTest, ltdTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
sqlHelper = new DatabaseHelper(getApplicationContext());
}
@Override
public void onResume() {
super.onResume();
db = sqlHelper.getReadableDatabase();
userCursor = db.query("geotable", null, null, null, null, null, null);
if (userCursor.moveToFirst()) {
int imageColIndex = userCursor.getColumnIndex("image");
int lngColIndex = userCursor.getColumnIndex("lng");
int latlColIndex = userCursor.getColumnIndex("lat");
do {
imageTest = userCursor.getString(imageColIndex);
lngTest = userCursor.getDouble(lngColIndex);
ltdTest = userCursor.getDouble(latlColIndex);
}
while (userCursor.moveToNext());
}
else
userCursor.close();
}
@Override
public void onDestroy() {
super.onDestroy();
db.close();
userCursor.close();
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(ltdTest, lngTest), 16));
// You can customize the marker image using images bundled with
// your app, or dynamically generated bitmaps.
googleMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromPath(imageTest))
.anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.position(new LatLng(ltdTest, lngTest)));
}
加ArrayList中,
if (userCursor.moveToFirst()) {
int imageColIndex = userCursor.getColumnIndex("image");
int lngColIndex = userCursor.getColumnIndex("lng");
int latlColIndex = userCursor.getColumnIndex("lat");
do {
imageTest = userCursor.getString(imageColIndex);
lngTest = userCursor.getDouble(lngColIndex);
ltdTest = userCursor.getDouble(latlColIndex);
geoList.add(imageTest);
geoList.add(lngTest);
geoList.add(ltdTest);
}
while (userCursor.moveToNext());
}
然後顯示與週期標記
for (int i = 0; i < geoList.size(); i=i+3) {
googleMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromPath((String) geoList.get(i)))
.anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
.position(new LatLng((Double) geoList.get(i+2), (Double) geoList.get(i+1))));
}
在地圖上只顯示最後添加到DB標誌,我'加2-10標誌但見只有最後。
請給我更多關於aloop座標的信息 – YAndrii
我沒有正確的把數據放到數據庫中,所有的標記都設置爲table,其中id = 0; – YAndrii