问题描述
我想创建一个地图的活动,我已经能够成功,必须从主要活动读取坐标的静态字符串,但是这是很难codeD,我希望你将这些坐标转换到一个SQLite数据库和对它们进行读取。我曾尝试环顾四周,但我只是不能得到,我要寻找的答案,我会感激,如果有人可以帮助我,请
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
这是我的主要活动 - GMapsActivity.java:
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);
这是我的数据库创建者 - DatabaseManager.java
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();
}
}
我是新来的Android编程,所以我将不胜感激的是有人居然可以告诉我的code片段,而不是一般的方向,因为我真的不知道他们笑
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
编辑:我曾在一个SQLite浏览器中创建的数据库条目我该如何将能够使主要活动文件从文件中读取值?
I have created database entries in an SQLite browserHow would i be able to make the main activity file read the values from that file?
这在code会尝试拨打值,如一般的想法:SELECT * FROM数据库放入数组然后循环它
A general idea that the code would try and call the values such as:SELECT * FROM the DB Put into Arraythen 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 :/
我真的AP preciate任何形式的帮助
I would really appreciate any form of help
感谢你在前进
推荐答案
首先,创建一个类,数据库记录 DatabaseObject.java
的对象:
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;
}
}
那么在添加这两个功能,你的 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这个声明为全局变量:
1- Declare this as global variable:
DatabaseManager db = new DatabaseManager(this);
2 - 检索所有对象:
2- Retrieve all objects:
List<DatabaseObject> K = db.getAllDatabaseObject();
3的循环使用这种方式:
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);
}
这篇关于创建SQLite数据库来存储Geopoints和阅读他们到MapActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!