i am trying to create a Map Activity and i have been able to successfully have a static string of coordinates to be read from the main activity, however this is hard coded and i wish you insert these coordinates into an SQLite Database and for them to be read. I have tried looking around but i simply cant get the answer that i am looking for, and i would grateful if someone could help me out please
Heres my Main Activity - 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);
And this is my database creator - 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();
}
}
I am new to Android Programming so i would be grateful is someone could actually show me snippets of code as opposed to general directions since i dont really understand them lol
EDIT: I have created database entries in an SQLite browser How would i be able to make the main activity file read the values from that file?
A general idea that the code would try and call the values such as: SELECT * FROM the DB Put into Array then loop it
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);
Its just im really not sure how to implement this :/
I would really appreciate any form of help
Thank You in advance
First, create a class as an object of database record DatabaseObject.java
:
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;
}
}
Then add these two functions in your DatabaseManager.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;
}
And now use these in any activity like this:
1- Declare this as global variable:
DatabaseManager db = new DatabaseManager(this);
2- Retrieve all objects:
List<DatabaseObject> K = db.getAllDatabaseObject();
3- Use it in loop this way:
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);
}