0

I have difficulties when they want to give a different name and a different marker images on Google Maps Marker, I use SQLite database to store the data from the marker, the following code from my project :

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

    }

}

Then I would do different Marker calling on my other layout in the form of maps, but how do I enter data in the layout to display the marker that I insert in the database? The following layout will I enter along with the marker maps :

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

Please help, thank you :)

Long Cat

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.access$600(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)

1 Answers1

0

I have two ideas.

  1. You can query TABLE_GEOPOINT again, clear map and all markers again

    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();
    
    }
    
  2. You can keep a reference of the ids of new markers added to the database

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

EDIT

To add more columns to your database you need to modify your create statement

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

Then when you do the query just get the new columns value

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

And set that values as your markers name and icon

Marker marqueur = map.addMarker(new MarkerOptions()
                        .position(
                                new LatLng(Double.parseDouble(latitude),
                                        Double.parseDouble(longitude)))
                        .title(name)
                        .icon(iconDecoded);

IMPORTANT

Note that iconEncoded is a string and the marker image is a 'BitmapDescriptor' so you first need to convert iconEncoded into a Bitmap and that bitmap into a BitmapDescriptor before setting it as the marker icon. I hope you know how to do this already, If not there are lots of posts related to that and I'm sure you'll find the way there.

Another important thing is that you need to delete your application data before creating the new columns in your database or your app will crash. The simplest way to do it is uninstall your app and run it again. This only needs to be done once.

Community
  • 1
  • 1
Carlos J
  • 2,965
  • 4
  • 17
  • 28
  • I mean in the layout, if I use my code, all the markers in the layout called "Bonjour Tunis", however I want, another one with his marker have different names when displayed @CarlosJ – Rodiah Assegaf Feb 11 '15 at 17:30
  • So why don't you add another column to your database table with the desired name? So when you read the database you can retrieve those names. – Carlos J Feb 11 '15 at 17:32
  • on values.put, do I have to include COL_NAME and COL_IMAGE ?? – Rodiah Assegaf Feb 11 '15 at 18:08
  • Correct! And again you have to encode your image into a String. It would be the reverse process of converting the String into a Bitmap – Carlos J Feb 11 '15 at 18:14
  • how to? i do not understand @CarlosJ – Rodiah Assegaf Feb 11 '15 at 19:11
  • Check [this post](http://stackoverflow.com/questions/13562429/how-many-ways-to-convert-bitmap-to-string-and-vice-versa) – Carlos J Feb 11 '15 at 19:12
  • I have not used the code before you give, but still uses the original, there is an error when I run it, look longcat post @CarlosJ – Rodiah Assegaf Feb 11 '15 at 19:21
  • Did it run before??? What did you change? try changing the name of your table from marquer.db to marker – Carlos J Feb 11 '15 at 19:30
  • I just tried it, still using the original, but why can not ?? @CarlosJ – Rodiah Assegaf Feb 11 '15 at 19:33
  • where I changed the name of his table? @CarlosJ – Rodiah Assegaf Feb 11 '15 at 19:41
  • Listen as much as I'd like to help you obviously need a full tutorial on databases. I could write it here, but that's not the point as creation of a database is unrelated to the original question. SO why don't you check [this tutorial](http://www.vogella.com/tutorials/AndroidSQLite/article.html) and [this one too](http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/) they will explain how to get your database running and once you have you have done that you can do the markers thing that you want. But first focus on master databases the rest will flow after that – Carlos J Feb 11 '15 at 19:47