2012-02-25 72 views
2

我想創建一個Map Activity,並且我已經能夠成功地從主活動讀取一個靜態的座標字符串,但是這是硬編碼的,我希望你插入這些座標到一個SQLite數據庫,併爲他們閱讀。 我試圖四處尋找,但我根本無法得到我要找的答案,我會感激,如果有人可以幫助我,請創建一個SQLite數據庫來存儲Geopoint並將它們讀到MapActivity上

繼承人我主要活動 - GMapsActivity.java:

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mapView = (MapView)findViewById(R.id.map_view); 
    mapView.setBuiltInZoomControls(true); 
    //mapView.setStreetView(true); 
    mapController = mapView.getController(); 

    String coordinates[] = {"51.52241608253253","-0.1318359375"}; 
    double lat = Double.parseDouble(coordinates[0]); 
    double lng = Double.parseDouble(coordinates[1]); 
    GeoPoint general = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
    mapController.setZoom(10); 
    mapController.animateTo(general); 

    mapOverlays = mapView.getOverlays(); 
    drawable = this.getResources().getDrawable(R.drawable.blue); 
    itemizedOverlay = new NewItemizedOverlay(drawable, this); 

    GeoPoint point = new GeoPoint((int)(51.555890943494276*1E6), (int)(-0.39989858865737915*1E6)); 
    OverlayItem overlayitem = new OverlayItem(point, "Greenword Veterinary" , "57 Station Approach, South Ruislip, Ruislip, Middlesex, HA4 6SL, 020 8845 8144"); 
    itemizedOverlay.addOverlay(overlayitem); 

    GeoPoint point2 = new GeoPoint((int)(51.598707*1E6), (int)(-0.393416*1E6)); 
    OverlayItem overlayitem2 = new OverlayItem(point2, "MediVet Pinner" , "2A Pinner Green, Pinner, Middlesex, HA5 2AA, 020 8866 0727"); 
    itemizedOverlay.addOverlay(overlayitem2); 

    GeoPoint point3 = new GeoPoint((int)(51.670865*1E6), (int)(-0.397034*1E6)); 
    OverlayItem overlayitem3 = new OverlayItem(point3, "MediVet Watford" , "237 Saint Albans Road, Watford, Hertfordshire WD24 5BP, 01923 243 429"); 
    itemizedOverlay.addOverlay(overlayitem3); 
    mapOverlays.add(itemizedOverlay); 

    myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 

這是我的數據庫創建者 - DatabaseManager.java

package com.javacodegeeks.android.googlemaps; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseManager { 

public static final String KEY_ID = "_id"; 
public static final String KEY_LAT = "vetlat"; 
public static final String KEY_LONGI = "vetlongi"; 
public static final String KEY_NAME = "vetname"; 
public static final String KEY_INFO = "vetinfo"; 

private static final String DATABASE_NAME = "myvet.db"; 
private static final String DATABASE_TABLE = "vetLocations"; 
private static final int DATABASE_VERSION = 1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDB; 

private static class DbHelper extends SQLiteOpenHelper 
{ 

    public DbHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
       KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       KEY_LAT + "REAL NOT NULL," + 
       KEY_LONGI + "REAL NOT NULL," + 
       KEY_NAME + "TEXT NOT NULL," + 
       KEY_INFO + "TEXT NOT NULL);" 
     ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 

} 

public DatabaseManager(Context c) 
{ 
    ourContext = c; 
} 

public DatabaseManager open() 
{ 
    ourHelper = new DbHelper(ourContext); 
    ourDB = ourHelper.getReadableDatabase(); 
    return this; 
} 

public void close() 
{ 
    ourHelper.close(); 
} 

}

我是新來的Android編程,所以我會感恩是一個人實際上可以告訴我的代碼片段,而不是一般的方向,因爲我真的不知道他們笑

編輯: 我已經在SQLite瀏覽器 創建數據庫條目我將如何能夠使主要活動文件讀取該文件中的值?

一個總體思路,代碼會嘗試撥打值,如:投入陣列 然後循環它

points = ((int)([lat_value_from_db1*E6]), ((int)([longi_value_from_db1*E6]); 
overlay = (points, [name_value_from_db], [info_value_from_db]); 
itemizedOverylay.addOverlay(allOverlays); 

它只是我真的不知道如何實現這個 SELECT * FROM的DB :/

我真的很感激任何形式的幫助

預先感謝您

回答

3

首先,創建一個類DatabaseObject.java數據庫記錄的對象:

// Getting one database object by id 
public DatabaseObject get_DatabaseObject(int id) { 
     SQLiteDatabase db = this.getReadableDatabase(); 

     Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID, 
       KEY_LAT, KEY_LONGI,KEY_NAME,KEY_INFO }, KEY_ID + "=?", 
       new String[] { String.valueOf(id) }, null, null,null,null); 
     if (cursor != null) 
      cursor.moveToFirst(); 

     DatabaseObject data = new DatabaseObject(Integer.parseInt(cursor.getString(0)), 
       Double.parseDouble(cursor.getString(1)), Double.parseDouble(cursor.getString(2)),cursor.getString(3), cursor.getString(4)); 
     // return database object 
     return data; 
    } 

    // Getting All database objects 
    public List<DatabaseObject> getAllDatabaseObject() { 
     List<DatabaseObject> contactList = new ArrayList<DatabaseObject>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_NAME; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       DatabaseObject data = new DatabaseObject(); 
       data.setID(Integer.parseInt(cursor.getString(0))); 
       data.setLAT(Double.parseDouble(cursor.getString(1))); 
       data.setLONGI(Double.parseDouble(cursor.getString(2))); 
       data.setNAME(cursor.getString(3)); 
       data.setINFO(cursor.getString(4)); 

       // Adding contact to list 
       contactList.add(data); 
      } while (cursor.moveToNext()); 
     } 

     // return database object list 
     return dataList; 
    } 

在任何像這樣的活動,現在使用這些:

public class DatabaseObject { 

     int _id; 
     double _lat; 
     double _lng; 
     String _name; 
     String _info; 

     // Empty constructor 
     public DatabaseObject(){ 

     } 
     // constructor 
     public DatabaseObject(int id, double lat, double lng,String name ,String info){ 
      this._id = id; 
      this._lat=lat; 
      this._lng=lng; 
      this._name=name; 
      this._info=info; 
     } 
     public DatabaseObject(double lat, double lng,String name ,String info){ 
      this._lat=lat; 
       this._lng=lng; 
       this._name=name; 
       this._info=info; 
     } 
     // constructor 

     // getting ID 
     public int getID(){ 
      return this._id; 
     } 

     // setting id 
     public void setID(int id){ 
      this._id = id; 
     } 


     public double getlat(){ 
      return this._lat; 
     } 

     public void setlat(double lat){ 
      this._lat = lat; 
     } 

     public double getlng(){ 
      return this._lng; 
     } 

     public void setlng(double lng){ 
      this._lng = lng; 
     } 

     public String getname(){ 
      return this._name; 
     } 

     // setting id 
     public void setname(String name){ 
      this._name = name; 
     } 

     public String getinfo(){ 
      return this._info; 
     } 

     // setting id 
     public void setinfo(String info){ 
      this._infor= info; 
     } 
} 

然後在你的DatabaseManager.java添加這兩種功能

1-將此聲明爲全局變量:

DatabaseManager db = new DatabaseManager(this); 

2-檢索所有對象:

List<DatabaseObject> K = db.getAllDatabaseObject(); 

3-用它,在循環是這樣的:

for (DatabaseObject cn : K) { 
     point = new GeoPoint((int)(cn.getlat()*E6), (int)(cn.getlng()*E6)); 
     overlayitem = new OverlayItem(point, cn.getname(), cn.getinfo()); 
     itemizedOverlay.addOverlay(overlayitem); 
     mapOverlays.add(itemizedOverlay); 
} 
+0

非常感謝你確實幫助,非常感謝:) – Ibz786 2012-03-25 20:42:01

+0

沒問題的伴侶,快樂編碼! – 2012-03-26 00:07:23

相關問題