我想通过从SQLite中检索经度和纬度来显示用户在谷歌地图上的GPS位置。请告诉我在Google地图上显示来自SQLite的信息的程序。这是我用来保存经度和纬度的代码。我也在使用这个链接 http://www.androidhive.info/2012/01/android-working-with-google-maps/ 对于地图,但不知道如何从SQLite中检索经度和纬度。 MAiN ACTIVITY:
public class MainActivity extends Activity {
ListView list;
mylocation loc = new mylocation();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager mylocman = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener myloclist = new mylocation();
mylocman.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myloclist);
loc.updateDatabase(this);
GPSdatabase myDatabase=new GPSdatabase(this);
myDatabase.open();
Cursor cursor=myDatabase.getAllRows();
cursor.moveToFirst();
ArrayList listContents = new ArrayList();
for (int i = 0; i < cursor.getCount(); i++)
{
listContents.add("Lat=" +cursor.getString(1) +" "+"Log "+ cursor.getString(2));
cursor.moveToNext(); } myDatabase.close();
ListAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, listContents);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
/*public void updateDatabase(){
GPSDatabase myDatabase=new GPSDatabase(context);
myDatabase.open();
myDatabase.insertRow(lat.substring(0,4),log.substring(0,4));
myDatabase.close();
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的位置calss:
public class mylocation implements LocationListener {
String lat=null;
String log=null;
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String text="my latitude="+location.getLatitude()+"longitude="+location.getLongitude();
lat=location.getLatitude()+"";
log=location.getLongitude()+"";
//updateDatabase();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void updateDatabase(Context context)
{
if(lat!=null || log!=null)
{
GPSdatabase myDatabase=new GPSdatabase(context);
myDatabase.open();
myDatabase.insertRows(lat.substring(0,4),log.substring(0,4));
myDatabase.close();
}
}
}
我的数据库类:
公共类GPS数据库{
private Context context;
private DbHelper dbHelper;
public final String DBNAME="gps1";
public final int DBVERSION=3;
public SQLiteDatabase db;
public final String COLUMN2="latitude";
public final String COLUMN3="longitude";
public final String COLUMN1="locationId";
public final String TABLENAME="location";
public final String CREATERDB="create table location(locationId integer primary key autoincrement, latitude text not null, longitude text not null);";
public GPSdatabase(Context context){
this.context=context;
dbHelper=new DbHelper(context);
}
public class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context){
super(context,DBNAME,null,DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE = "CREATE TABLE " + "location" + "("
+ "latitude" + " TEXT,"
+ "longitude" + " TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public long insertRows(String column2, String column3){
ContentValues value=new ContentValues();
value.put(COLUMN2, column2);
value.put(COLUMN3, column3);
return db.insert(TABLENAME,null,value);
}
public Cursor getAllRows(){
Cursor cursor=db.query(TABLENAME, new String[]{COLUMN1,COLUMN2,COLUMN3}, null,null, null, null, null);
return cursor;
}
public void open() throws SQLException{
db = dbHelper.getWritableDatabase();
//return true;
}
public void close(){
dbHelper.close();
//return true;
}}
答案 0 :(得分:0)
为什么不直接显示用户的位置而不保存和来回检索? 如果您告诉地图显示用户位置,则会自动完成: http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#setMyLocationEnabled(boolean)
map.setMyLocationEnabled(true);