我有 SQlite DB,目前我有 4 个 La/Long 和 Names 值我关注了这个链接,但没有帮助从 DB 获取多个引脚我想在 MapView 和我的主类上获取多个引脚我已经完成了这个编码:
package com.example.demo;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import com.example.tabhost.MyTabHostProvider;
import com.example.tabhost.TabHostProvider;
import com.example.tabhost.TabView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MapViewActivity extends MapActivity{
private MapView mapView;
public ArrayList lati,longi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHostProvider tabProvider = new MyTabHostProvider(MapViewActivity.this);
TabView tabView = tabProvider.getTabHost("Map");
tabView.setCurrentView(R.layout.map);
setContentView(tabView.render(2));
mapView = (MapView)findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
// --- Cursor Coding
Cursor cursor = myDbHelper.getAllContacts();
// int a = cursor.getCount();
// Log.d("GH:", String.valueOf(a));
String[] names = cursor.getColumnNames();
System.out.print(names[1]);
Log.v("Name :", names[3]);
lati = new ArrayList();
longi = new ArrayList();
String latitudes = "";
String longitudes = "";
String result = "";
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
latitudes = latitudes + cursor.getDouble(3) + "\n";
longitudes = longitudes + cursor.getDouble(4) + "\n";
result = result + cursor.getString(2) + "\n";
// Log.d("Latitude222 :", latitudes);
}
lati.add(latitudes);
longi.add(longitudes);
int b = lati.size(); // returns 1
Log.d("Size :", String.valueOf(b));
// List<Overlay> mapOverlays = mapView.getOverlays();
// Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
// MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable, this);
for (int i=0; i<b; i++){
// Log.d("Hello :", String.valueOf(al.get(i)));
Log.d("Hello :", (String) lati.get(i));
// GeoPoint point2 = new GeoPoint(35410000, 139460000);
// OverlayItem overlayitem2 = new OverlayItem(point2, "Hi!", "I'm in Japan!");
// itemizedoverlay.addOverlay(overlayitem2);
}
Log.d("Hello22 :", (String) lati.get(0)); // here i am getting all the 4 values of latitude at zero index :(
// GeoPoint point = new GeoPoint((int)(Double.parseDouble((String) lati.get(1)) * 1E6),(int)(Double.parseDouble((String) longi.get(1)) * 1E6));
// OverlayItem overlayitem = new OverlayItem(point, "Hello!", "I'm in Mexico City!");
// itemizedoverlay.addOverlay(overlayitem);
// mapOverlays.add(itemizedoverlay);
cursor.close();
// ---- End Cursor coding
}catch(SQLException sqle){
throw sqle;
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
我的 DB Helper 类有这个代码
............... private SQLiteDatabase myDataBase;
private final Context myContext;
private DataBaseHelper ourHelper;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
Log.v("DB:", "Exist");
//do nothing - database already exist
}else{
Log.v("DB:", "Else is called");
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
Log.v("DB:", "copy DB");
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Log.v("DB:", "Opened");
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
Log.v("DB:", "Closed");
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getAllContacts()
{
return myDataBase.query("Points", new String[] {"_id", "p_id", "name", "latitude", "longitude"},
null, null, null, null, null);
}
}
我的 MapItemizedOverlay 类是这样的:
package com.example.demo;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class MapItemizedOverlay extends ItemizedOverlay {
Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MapItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
// return null;
}
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
// return 0;
}
public MapItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
请任何人告诉我我需要在我的主地图类中进行哪些更正才能获得 MapView 上的所有图钉。