【问题标题】:show GPS location on google map在谷歌地图上显示 GPS 位置
【发布时间】:2014-10-22 05:39:00
【问题描述】:

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

主要活动:

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

我的位置类:

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


    }

我的数据库类:

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;


 }
}


【问题讨论】:

    标签: sqlite google-maps gps


    【解决方案1】:

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

    map.setMyLocationEnabled(true);
    

    【讨论】:

    • 我需要保存我的数据库信息以备将来使用
    • 好的,但是您可以保存它,但会自动显示位置,而无需轮询数据库以获得可以轻松管理和滞后的位置!
    • 不,我想添加 Google api,然后想通过 SQLITE 在 google 地图上显示位置
    • 这就是我想做的一切
    • 所以你需要在 db 表上有一个“池”,得到位置 X 和 Y 是 Lon Lat 双值。您只需第一次添加一个标记,然后使用 developer.android.com/reference/com/google/android/gms/maps/… 更改该标记的位置
    猜你喜欢
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多