2014-10-22 117 views
0

我想通过从SQLite中检索经度和纬度来显示用户在Google地图上的GPS位置。请告诉我在Google地图上显示来自SQLite的信息的过程。这里是我用来保存经度和纬度的代码。我也使用链接 http://www.androidhive.info/2012/01/android-working-with-google-maps/ 地图,但不知道如何从SQLite中检索经度和纬度。 主要活动:在谷歌地图上显示GPS位置

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


    } 

我的数据库类:

公共类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; 


     }} 

回答

0

为什么不直接显示用户的位置而不保存和来回检索? 如果你告诉地图来显示用户的位置,它是自动完成的: http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#setMyLocationEnabled(boolean)

map.setMyLocationEnabled(true); 
+0

我需要保存,因为保持供将来使用 – 2014-10-22 07:35:45

+0

好我的数据库信息,但你可以将它保存,但显示自动定位,无需轮询分贝的位置,可以毫不费力地管理和滞后! – 2014-10-22 07:37:20

+0

否我想添加谷歌API,然后想通过谷歌地图上的SQLITE显示位置 – 2014-10-22 12:06:42