我想創建一個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 :/
我真的很感激任何形式的幫助
預先感謝您
非常感謝你確實幫助,非常感謝:) – Ibz786 2012-03-25 20:42:01
沒問題的伴侶,快樂編碼! – 2012-03-26 00:07:23