如何在使用 SQLite 的 Google 地图标记 API 上提供名称和不同的图像?
How do I give a name and a different image on Google Maps Marker API that uses SQLite?
当他们想在 Google Maps Marker 上提供不同的名称和不同的标记图像时,我遇到了困难,我使用 SQLite 数据库存储来自标记的数据,以下代码来自我的项目:
MaBase.java
public class MaBase extends SQLiteOpenHelper {
private static final String TABLE_MARK ="marqueur.db";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_MARK + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONG + " TEXT NOT NULL, " +COL_LAT+" TEXT NOT NULL);";
public MaBase (Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//on créé la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_BDD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE " + TABLE_MARK + ";");
onCreate(db);
}
}
MainActivity.java
public class MainActivity extends Activity {
private static final String NOM_BDD = "marqueur.db";
private static final String TABLE_GEOPOINT = "geopoint";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";
static final LatLng TUNIS = new LatLng(36.894883, 10.1432776); // https://www.google.tn/maps/@36.794883,10.1432776,9z
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// fct pour remplire la base avec qlq points pour le demo :)
sauver_point();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
Cursor c = db.query(TABLE_GEOPOINT, new String[] { COL_ID, COL_LONG,
COL_LAT }, null, null, null, null, null, null);
int col = c.getCount(); // col=0 pas de enregistrement qui verifie la
// condition
if (col == 0) {
Toast.makeText(MainActivity.this, "Pas de donnees ",
Toast.LENGTH_LONG).show();
// effacer le contenue champ login et mot de passe
} else {
c.moveToFirst();
while (c.isAfterLast() == false) {
// conversion int to string casting
String id = "" + c.getInt(0);
String longitude = c.getString(1);
String latitude = c.getString(2);
Marker marqueur = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble(latitude),
Double.parseDouble(longitude)))
.title("Bonjour Tunis")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark2)));
c.moveToNext();
}
}
c.close();
db.close();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(TUNIS, 12.0f));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// pour ajouter des points ala base sqlite juste pour le demo :)
void sauver_point() {
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(COL_ID , "1");
values.put(COL_LAT, "36.830722");
values.put(COL_LONG, "10.165672");
db.insert(TABLE_GEOPOINT, null, values);
// creer un autre utilisateur
values = new ContentValues();
values.put(COL_LAT , "36.830922");
values.put(COL_LONG, "10.275572");
db.insert(TABLE_GEOPOINT, null, values);
values = new ContentValues();
values.put(COL_LAT, "36.930522");
values.put(COL_LONG, "10.385572");
db.insert(TABLE_GEOPOINT, null, values);
values = new ContentValues();
values.put(COL_LAT, "36.750422");
values.put(COL_LONG, "10.495572");
db.insert(TABLE_GEOPOINT, null, values);
values.put(COL_LAT, "36.936422");
values.put(COL_LONG, "11.495572");
db.insert(TABLE_GEOPOINT, null, values);
values.put(COL_LAT, "36.990422");
values.put(COL_LONG, "9.995572");
db.insert(TABLE_GEOPOINT, null, values);
db.close();
}
}
然后我会做不同的标记以地图的形式调用我的其他布局,但是我如何在布局中输入数据以显示我插入数据库的标记?以下布局将与标记图一起输入:
fragment_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_distance_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
MapsFragment.java
public class GamesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_maps, container, false);
return rootView;
}
}
请帮忙,谢谢:)
长猫
02-12 02:15:33.851: E/AndroidRuntime(2489): FATAL EXCEPTION: main
02-12 02:15:33.851: E/AndroidRuntime(2489): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bongkorr/com.bongkorr.maps.MainActivity}: android.database.sqlite.SQLiteException: unknown database marqueur (code 1): , while compiling: CREATE TABLE marqueur.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, LONGITUDE TEXT NOT NULL, LATITUDE TEXT NOT NULL);
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.access0(ActivityThread.java:141)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.os.Looper.loop(Looper.java:137)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-12 02:15:33.851: E/AndroidRuntime(2489): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 02:15:33.851: E/AndroidRuntime(2489): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-12 02:15:33.851: E/AndroidRuntime(2489): at dalvik.system.NativeStart.main(Native Method)
02-12 02:15:33.851: E/AndroidRuntime(2489): Caused by: android.database.sqlite.SQLiteException: unknown database marqueur (code 1): , while compiling: CREATE TABLE marqueur.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, LONGITUDE TEXT NOT NULL, LATITUDE TEXT NOT NULL);
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.bongkorr.maps.MaBase.onCreate(MaBase.java:31)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.bongkorr.maps.MainActivity.sauver_point(MainActivity.java:86)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.bongkorr.maps.MainActivity.onCreate(MainActivity.java:35)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.Activity.performCreate(Activity.java:5104)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
我有两个想法。
您可以再次查询TABLE_GEOPOINT,再次清除地图和所有标记
public void reloadMarkers() {
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
Cursor c = db.query(TABLE_GEOPOINT, new String[] { COL_ID, COL_LONG,
COL_LAT }, null, null, null, null, null, null);
int col = c.getCount(); // col=0 pas de enregistrement qui verifie la
// condition
if (col == 0) {
Toast.makeText(MainActivity.this, "Pas de donnees ",
Toast.LENGTH_LONG).show();
// effacer le contenue champ login et mot de passe
} else {
map.clear(); //Delete all markers in map
//Add them all again, including the new ones
c.moveToFirst();
while (c.isAfterLast() == false) {
// conversion int to string casting
String id = "" + c.getInt(0);
String longitude = c.getString(1);
String latitude = c.getString(2);
Marker marqueur = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble(latitude),
Double.parseDouble(longitude)))
.title("Bonjour Tunis")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark2)));
c.moveToNext();
}
}
c.close();
db.close();
}
您可以保留添加到数据库中的新标记的 ID 的参考
void sauver_point() {
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(COL_ID , "1");
values.put(COL_LAT, "36.830722");
values.put(COL_LONG, "10.165672");
long id = db.insert(TABLE_GEOPOINT, null, values); //Get the db id of the new inserted marker
//You can keep the id in an array and then query your database looking for those specific ids.
//The just add them to your map. Just don't clear the map his time! Or you will loose the old ones
db.close();
}
编辑
要向数据库添加更多列,您需要修改创建语句
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_MARK + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONG + " TEXT NOT NULL, " +COL_LAT+" TEXT NOT NULL, "+COL_NAME+" VARCHAR(250) NOT NULL, " +COL_IMAGE+" TEXT NOT NULL);";
然后当您进行查询时,只需获取新的列值
String id = "" + c.getInt(0);
String longitude = c.getString(1);
String latitude = c.getString(2);
String name = c.getString(3);
String iconEncoded = c.getString(4);
并将该值设置为您的标记名称和图标
Marker marqueur = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble(latitude),
Double.parseDouble(longitude)))
.title(name)
.icon(iconDecoded);
重要
请注意,iconEncoded
是一个字符串,标记图像是 'BitmapDescriptor',因此您首先需要将 iconEncoded
转换为 Bitmap
,然后将该位图转换为 BitmapDescriptor
,然后将其设置为标记图标。我希望你已经知道如何做到这一点,如果不知道的话,有很多相关的帖子,我相信你会找到方法的。
另一件重要的事情是,您需要在数据库中创建新列之前删除您的应用程序数据,否则您的应用程序将崩溃。最简单的方法是卸载您的应用,然后 运行 重新安装。这只需要完成一次。
当他们想在 Google Maps Marker 上提供不同的名称和不同的标记图像时,我遇到了困难,我使用 SQLite 数据库存储来自标记的数据,以下代码来自我的项目:
MaBase.java
public class MaBase extends SQLiteOpenHelper {
private static final String TABLE_MARK ="marqueur.db";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_MARK + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONG + " TEXT NOT NULL, " +COL_LAT+" TEXT NOT NULL);";
public MaBase (Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//on créé la table à partir de la requête écrite dans la variable CREATE_BDD
db.execSQL(CREATE_BDD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE " + TABLE_MARK + ";");
onCreate(db);
}
}
MainActivity.java
public class MainActivity extends Activity {
private static final String NOM_BDD = "marqueur.db";
private static final String TABLE_GEOPOINT = "geopoint";
private static final String COL_ID = "ID";
private static final String COL_LONG = "LONGITUDE";
private static final String COL_LAT = "LATITUDE";
static final LatLng TUNIS = new LatLng(36.894883, 10.1432776); // https://www.google.tn/maps/@36.794883,10.1432776,9z
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// fct pour remplire la base avec qlq points pour le demo :)
sauver_point();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
Cursor c = db.query(TABLE_GEOPOINT, new String[] { COL_ID, COL_LONG,
COL_LAT }, null, null, null, null, null, null);
int col = c.getCount(); // col=0 pas de enregistrement qui verifie la
// condition
if (col == 0) {
Toast.makeText(MainActivity.this, "Pas de donnees ",
Toast.LENGTH_LONG).show();
// effacer le contenue champ login et mot de passe
} else {
c.moveToFirst();
while (c.isAfterLast() == false) {
// conversion int to string casting
String id = "" + c.getInt(0);
String longitude = c.getString(1);
String latitude = c.getString(2);
Marker marqueur = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble(latitude),
Double.parseDouble(longitude)))
.title("Bonjour Tunis")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mark2)));
c.moveToNext();
}
}
c.close();
db.close();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(TUNIS, 12.0f));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
// pour ajouter des points ala base sqlite juste pour le demo :)
void sauver_point() {
MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1);
SQLiteDatabase db = maBaseSQLite.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(COL_ID , "1");
values.put(COL_LAT, "36.830722");
values.put(COL_LONG, "10.165672");
db.insert(TABLE_GEOPOINT, null, values);
// creer un autre utilisateur
values = new ContentValues();
values.put(COL_LAT , "36.830922");
values.put(COL_LONG, "10.275572");
db.insert(TABLE_GEOPOINT, null, values);
values = new ContentValues();
values.put(COL_LAT, "36.930522");
values.put(COL_LONG, "10.385572");
db.insert(TABLE_GEOPOINT, null, values);
values = new ContentValues();
values.put(COL_LAT, "36.750422");
values.put(COL_LONG, "10.495572");
db.insert(TABLE_GEOPOINT, null, values);
values.put(COL_LAT, "36.936422");
values.put(COL_LONG, "11.495572");
db.insert(TABLE_GEOPOINT, null, values);
values.put(COL_LAT, "36.990422");
values.put(COL_LONG, "9.995572");
db.insert(TABLE_GEOPOINT, null, values);
db.close();
}
}
然后我会做不同的标记以地图的形式调用我的其他布局,但是我如何在布局中输入数据以显示我插入数据库的标记?以下布局将与标记图一起输入:
fragment_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_distance_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
MapsFragment.java
public class GamesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_maps, container, false);
return rootView;
}
}
请帮忙,谢谢:)
长猫
02-12 02:15:33.851: E/AndroidRuntime(2489): FATAL EXCEPTION: main
02-12 02:15:33.851: E/AndroidRuntime(2489): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bongkorr/com.bongkorr.maps.MainActivity}: android.database.sqlite.SQLiteException: unknown database marqueur (code 1): , while compiling: CREATE TABLE marqueur.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, LONGITUDE TEXT NOT NULL, LATITUDE TEXT NOT NULL);
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.access0(ActivityThread.java:141)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.os.Looper.loop(Looper.java:137)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-12 02:15:33.851: E/AndroidRuntime(2489): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 02:15:33.851: E/AndroidRuntime(2489): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-12 02:15:33.851: E/AndroidRuntime(2489): at dalvik.system.NativeStart.main(Native Method)
02-12 02:15:33.851: E/AndroidRuntime(2489): Caused by: android.database.sqlite.SQLiteException: unknown database marqueur (code 1): , while compiling: CREATE TABLE marqueur.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, LONGITUDE TEXT NOT NULL, LATITUDE TEXT NOT NULL);
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.bongkorr.maps.MaBase.onCreate(MaBase.java:31)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.bongkorr.maps.MainActivity.sauver_point(MainActivity.java:86)
02-12 02:15:33.851: E/AndroidRuntime(2489): at com.bongkorr.maps.MainActivity.onCreate(MainActivity.java:35)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.Activity.performCreate(Activity.java:5104)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-12 02:15:33.851: E/AndroidRuntime(2489): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
我有两个想法。
您可以再次查询TABLE_GEOPOINT,再次清除地图和所有标记
public void reloadMarkers() { MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1); SQLiteDatabase db = maBaseSQLite.getWritableDatabase(); Cursor c = db.query(TABLE_GEOPOINT, new String[] { COL_ID, COL_LONG, COL_LAT }, null, null, null, null, null, null); int col = c.getCount(); // col=0 pas de enregistrement qui verifie la // condition if (col == 0) { Toast.makeText(MainActivity.this, "Pas de donnees ", Toast.LENGTH_LONG).show(); // effacer le contenue champ login et mot de passe } else { map.clear(); //Delete all markers in map //Add them all again, including the new ones c.moveToFirst(); while (c.isAfterLast() == false) { // conversion int to string casting String id = "" + c.getInt(0); String longitude = c.getString(1); String latitude = c.getString(2); Marker marqueur = map.addMarker(new MarkerOptions() .position( new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude))) .title("Bonjour Tunis") .icon(BitmapDescriptorFactory .fromResource(R.drawable.mark2))); c.moveToNext(); } } c.close(); db.close(); }
您可以保留添加到数据库中的新标记的 ID 的参考
void sauver_point() { MaBase maBaseSQLite = new MaBase(MainActivity.this, NOM_BDD, null, 1); SQLiteDatabase db = maBaseSQLite.getWritableDatabase(); ContentValues values = new ContentValues(); // values.put(COL_ID , "1"); values.put(COL_LAT, "36.830722"); values.put(COL_LONG, "10.165672"); long id = db.insert(TABLE_GEOPOINT, null, values); //Get the db id of the new inserted marker //You can keep the id in an array and then query your database looking for those specific ids. //The just add them to your map. Just don't clear the map his time! Or you will loose the old ones db.close(); }
编辑
要向数据库添加更多列,您需要修改创建语句
private static final String CREATE_BDD = "CREATE TABLE " + TABLE_MARK + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_LONG + " TEXT NOT NULL, " +COL_LAT+" TEXT NOT NULL, "+COL_NAME+" VARCHAR(250) NOT NULL, " +COL_IMAGE+" TEXT NOT NULL);";
然后当您进行查询时,只需获取新的列值
String id = "" + c.getInt(0);
String longitude = c.getString(1);
String latitude = c.getString(2);
String name = c.getString(3);
String iconEncoded = c.getString(4);
并将该值设置为您的标记名称和图标
Marker marqueur = map.addMarker(new MarkerOptions()
.position(
new LatLng(Double.parseDouble(latitude),
Double.parseDouble(longitude)))
.title(name)
.icon(iconDecoded);
重要
请注意,iconEncoded
是一个字符串,标记图像是 'BitmapDescriptor',因此您首先需要将 iconEncoded
转换为 Bitmap
,然后将该位图转换为 BitmapDescriptor
,然后将其设置为标记图标。我希望你已经知道如何做到这一点,如果不知道的话,有很多相关的帖子,我相信你会找到方法的。
另一件重要的事情是,您需要在数据库中创建新列之前删除您的应用程序数据,否则您的应用程序将崩溃。最简单的方法是卸载您的应用,然后 运行 重新安装。这只需要完成一次。