mercredi 3 juin 2015

How do I retrieve data latitude and longitude from sqlite database for use to put marker in google maps api

I have a sqlite database for saving name, address, category, latitudeandlongitude. In activity(1) i construct a listview. I have been able to retrieve data name for show in the listview, If the list is clicked it will open activity(2) which will showing data name, address, category and at the bottom there a map. I have been able to retrieve name, address and category in Activity(2) but i am confused to retrieve latitude and longitude for use to put marker in the map.

This my database class:

public class DB_Restoran extends SQLiteOpenHelper {
final static String DB_NAME = "db_restoran";

public DB_Restoran(Context context) {
    super(context, DB_NAME, null, 1);
    //Todo auto
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE IF NOT EXISTS restoran(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, category TEXT, address TEXT, latitude DOUBLE, longitude DOUBLE)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();
    values.put("_id", "1");
    values.put("name", "RM. Kemang Raya");
    values.put("category", "Umum");       
    values.put("address", "Jl.Endro Suratmin");        
    values.put("latitude", "-5.384402");
    values.put("longitude", "105.295443");
    db.insert("restoran", "_id", values);

    values.put("_id", "2");
    values.put("nama", "RM. Dua Saudara");
    values.put("category", "Masakan Padang");        
    values.put("address", "Jl.P.Tirtayasa Sukabumi");        
    values.put("latitude", "-5.384402");
    values.put("longitude", "105.295443");
    db.insert("restoran", "_id", values);

    values.put("_id", "3");
    values.put("name", "RM. Mbok Wito");
    values.put("category", "Umum");        
    values.put("address", "Jl.Arief Rahman Hakim");        
    values.put("latitude", "-5.384402");
    values.put("longitude", "105.295443");
    db.insert("restoran", "_id", values);

    values.put("_id", "4");
    values.put("name", "Babe Cafe");
    values.put("category", "Cafe");        
    values.put("address", "Jl.Arief Rahman Hakim");        
    values.put("latitude", "-5.384402");
    values.put("longitude", "105.295443");
    db.insert("restoran", "_id", values);

    values.put("_id", "5");
    values.put("name", "KFC Coffe");
    values.put("category", "Cepat Saji");        
    values.put("address", "Jl.Zaenal Abidin Pagar Alam");        
    values.put("latitude", "-5.384402");
    values.put("longitude", "105.295443");
    db.insert("restoran", "_id", values);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS restoran");
    onCreate(db);
}
}

This my Listview class(Activity1):

public class Menu_Restoran extends ActionBarActivity {
protected ListView lv;
protected ListAdapter adapter;    
SQLiteDatabase db;
Cursor cursor;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.menu_restoran);

    // enable up/back button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    db = (new DB_Restoran(this)).getWritableDatabase();
    lv = (ListView) findViewById(R.id.list);

    try {
        cursor = db.rawQuery("SELECT * FROM restoran ORDER BY name ASC", null);
        adapter = new SimpleCursorAdapter(this, R.layout.list_view, cursor,
                new String[]{"name", "category", "address", "latitude", "longitude"},
                new int[]{R.id.item, R.id.textView1, R.id.textView2, R.id.map});
        lv.setAdapter(adapter);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                detail(position);

            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void detail(int position) {        
    String _id = "";
    String name = "";
    String category = "";
    String address = "";    
    if (cursor.moveToFirst()) {
        cursor.moveToPosition(position);            
        name = cursor.getString(cursor.getColumnIndex("name"));
        category = cursor.getString(cursor.getColumnIndex("category"));
        address = cursor.getString(cursor.getColumnIndex("address"));



}

Intent iIntent = new Intent(this, DBResto_Parse.class);    
iIntent.putExtra("dataName", name);
iIntent.putExtra("dataCategory", category);
iIntent.putExtra("dataAddress", address);    
setResult(RESULT_OK, iIntent);
startActivityForResult(iIntent, 99);
}
}

This my parse class(Activity2):

public class DBResto_Parse extends ActionBarActivity {

TextView tv_name, tv_category, tv_address, id;        
Integer[] imageIDs = new Integer[3];  

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.restoran);

    // enable up/back button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent iIdentifikasi = getIntent();        
    String msg_name = iIdentifikasi.getStringExtra("dataName");
    String msg_category = iIdentifikasi.getStringExtra("dataCategory");
    final String msg_address = iIdentifikasi.getStringExtra("dataAdress"); 
    tv_name = (TextView) findViewById(R.id.tvName);
    tv_category = (TextView) findViewById(R.id.tvCategory);
    tv_address = (TextView) findViewById(R.id.tvAddress);               
    tv_name.setText(msg_name);
    tv_category.setText(msg_category);
    tv_address.setText(msg_address);

}
}

Have you guys can tell me to do this? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire