android - How do I retrieve data latitude and longitude from sqlite database for use to put marker in google maps api -
i have sqlite database
saving name
, address
, category
, latitude
andlongitude
. in activity(1) construct listview
. have been able retrieve data name show in listview, if list clicked open activity(2) showing data name, address, category , @ bottom there map
. have been able retrieve name, address , category in activity(2) confused retrieve latitude , longitude use put marker in map
.
this 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 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 * restoran order 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) { double latitude = 0; double longitude = 0; 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")); latitude = cursor.getdouble(cursor.getcolumnindex("latitude")); longitude = cursor.getdouble(cursor.getcolumnindex("longitude")); } intent iintent = new intent(this, dbresto_parse.class); iintent.putextra("dataname", name); iintent.putextra("datacategory", category); iintent.putextra("dataaddress", address); iintent.putextra("datalatitude", latitude); iintent.putextra("datalongitude", longitude); setresult(result_ok, iintent); startactivityforresult(iintent, 99); } }
this parse class(activity2):
public class dbresto_parse extends actionbaractivity { textview tv_name, tv_category, tv_address, id; double latitude; double longitude; private googlemap mmap; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.restoran); fragmentmanager myfragmentmanager = getsupportfragmentmanager(); supportmapfragment mysupportmapfragment = (supportmapfragment)myfragmentmanager.findfragmentbyid(r.id.map); mmap = mysupportmapfragment.getmap(); // enable up/back button getsupportactionbar().setdisplayhomeasupenabled(true); // create marker final markeroptions marker = new markeroptions().position(new latlng(latitude, longitude)).title("kfc").snippet("fast food").flat(true); // changing marker icon marker.icon(bitmapdescriptorfactory.fromresource(r.drawable.marker_resto)); // adding marker mmap.addmarker(marker); // construct cameraposition focusing on mountain view , animate camera position. cameraposition cameraposition = new cameraposition.builder() .target(new latlng(latitude, longitude)) // sets center of map mountain view .zoom(17) // sets zoom .bearing(90) // sets orientation of camera east .tilt(30) // sets tilt of camera 30 degrees .build(); // creates cameraposition builder mmap.animatecamera(cameraupdatefactory.newcameraposition(cameraposition)); intent iidentifikasi = getintent(); latitude = iidentifikasi.getdoubleextra("datalat", 0); longitude = iidentifikasi.getdoubleextra("datalng", 0); 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 guys can tell me this? in advance
can add latitude , longitude like
iintent.putextra("latitude", lat); iintent.putextra("longitude", lng);
if yes call following function want add marker
public void plotmarker(double latitude,double longitude){ googlemap.addmarker(new markeroptions().position(new latlng(latitude,longitude))); }
Comments
Post a Comment