samedi 30 mai 2015

How to adding marker from latitude and longitude i have in my sqlite database to google maps android

I have some data like name, address, type, image, lat and lng, in my sqlite db. I display the name and address in listview, if the list is clicked it will launching my db_parse.java or my new activity with display the name, address, type, image, lat and lng. Now i want to adding button "view in map" to the dp_parse.java for showing the location or add marker in google maps. How do I get the lat and lng value for the button and launching the map in maps activity?

This my DB 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, address TEXT, img BLOB, lat TEXT, lng TEXT)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();
    values.put("_id", "1");
    values.put("name", "RM. Raya");
    values.put("address", "Jl.Endro Suratmin Sukarame");
    values.put("img", R.drawable.ic_listrestro);
    values.put("lat", "-5.384402");
    values.put("lng", "105.295443");
    db.insert("restoran", "_id", values);

    values.put("_id", "2");
    values.put("name", "RM. Saudara");
    values.put("address", "Jl.P.Tirtayasa Sukabumi");
    values.put("img", R.drawable.dua_saudara);
    values.put("lat", "-5.384402");
    values.put("lng", "105.295443");
    db.insert("restoran", "_id", values);

    values.put("_id", "3");
    values.put("name", "RM. Wito");
    values.put("address", "Jl.Arief Rahman Hakim Blok A No.16");
    values.put("img", R.drawable.mbok_wito);
    values.put("lat", "-5.384402");
    values.put("lng", "105.295443");
    db.insert("restoran", "_id", values);

    values.put("_id", "4");
    values.put("name", "Cafe");
    values.put("address", "Jl.Arief Rahman Hakim Blok A No.29");
    values.put("img", R.drawable.cafe_babe);
    values.put("lat", "-5.384402");
    values.put("lng", "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 Menu Class

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", "address", "img"},
                    new int[]{R.id.item, R.id.textView1, R.id.icon});
            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) {
int img = 0;
String _id = "";
    String name = "";
    String address = "";
    String lat = "";
    String lng = "";
    if (cursor.moveToFirst()) {
        cursor.moveToPosition(position);
        img = cursor.getInt(cursor.getColumnIndex("img"));
        name = cursor.getString(cursor.getColumnIndex("name"));
        address = cursor.getString(cursor.getColumnIndex("address"));
        lat = cursor.getString(cursor.getColumnIndex("lat"));
        lng = cursor.getString(cursor.getColumnIndex("lng"));
    }

    Intent iIntent = new Intent(this, DBResto_Parse.class);
    iIntent.putExtra("dataIMG", img);
    iIntent.putExtra("dataName", name);
    iIntent.putExtra("dataAddress", address);
    iIntent.putExtra("dataLat", lat);
    iIntent.putExtra("dataLng", lng);
    setResult(RESULT_OK, iIntent);
    startActivityForResult(iIntent, 99);

}
}

This is my DB_Parse Class when i want to add button

public class DBResto_Parse extends ActionBarActivity {
    ImageView Img;
    TextView tv_name, tv_address, id;
    Gallery gallery;
    ImageSwitcher imageSwitcher;
    Integer[] imageIDs = new Integer[2];
    int msg_img;

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

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

        Button btnlihat = (Button) findViewById(R.id.lihatmap);
        btnlihat.setOnClickListener(new View.OnClickListener() {
//This is when a want to add button "view in map"

         
        Intent iIdentifikasi = getIntent();
        msg_img = iIdentifikasi.getIntExtra("dataIMG", 0);
        String msg_name = iIdentifikasi.getStringExtra("dataName");
        String msg_address = iIdentifikasi.getStringExtra("dataAddress");
        Img = (ImageView) findViewById(R.id.iv_detail);
        tv_name = (TextView) findViewById(R.id.tvName);
        tv_address = (TextView) findViewById(R.id.tvAddress);
        Img.setImageResource(msg_img);
        tv_name.setText(msg_name);
        tv_address.setText(msg_address);
    }
}

Thanks for your help

Aucun commentaire:

Enregistrer un commentaire