0
votes

I want to show the GPS location of the user on google maps by retrieving the longitude and latitude from SQLite. Please tell me the procedure to show the information from SQLite on Google map. Here is the code I am using for saving the longitude and latitude. And I am also using the link http://www.androidhive.info/2012/01/android-working-with-google-maps/ for maps but don't know how to retrieve the longitude and latitude from 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);
}}

My Location class:

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


    }

My DATABASE CLASS:

public class GPSdatabase {
 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;


 }
}


1

1 Answers

0
votes

Why not showing directly the location of the user without saving and retrieving back and forth? if you tell to the map to show user location, it is done automatically: http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#setMyLocationEnabled(boolean)

map.setMyLocationEnabled(true);