我有SQlite DB,目前我有4个La / Long和Names值 我跟着this链接,但没有帮助从DB获取多个引脚 我想在MapView和My Main Class上获得多个引脚我已完成此编码:
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;
}
}
请问任何人可以告诉我在Main Map类中需要进行哪些更正以获取MapView上的所有引脚。
答案 0 :(得分:0)
private class SitesOverlay extends ItemizedOverlay<CustomItem> {
private View view = null;
private List<CustomItem> items = new ArrayList<CustomItem>();
public SitesOverlay() {
super(null);
// array list = yout lat and long array size
for (int i = 0; i < arrayList.size(); i++) {
slat = Double.parseDouble(arrayList.get(i).getLat());
vlong = Double.parseDouble(arrayList.get(i).getvLong());
pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
Log.e("lat long", "--- "+slat);
MapviewActivity.this.mc.animateTo(pt);
items.add(new CustomItem(pt, arrayList.get(i).getBuissnessName(), "Bendigo", marker));
boundCenter(marker);
populate();
}
设置lat和long,如上面MapItemizedOverlay类中的代码
if(c.moveToFirst()){
do{
// YOUR CODE FOR EACH CURSOR ITEM HERE.
// the cursor moves one field at a time trhough it's whole dataset
}while(c.moveToNext())
}
答案 1 :(得分:0)
这可以帮助您在此功能中传递您的纬度和经度
for(int j= 0;j<arrofLatLOng.size();j++) {
String checklat = "your latitude"
String checklon = "your longitude"
point = new GeoPoint((int) ((Double.parseDouble(checklat) * 1E6)),(int)((Double.parseDouble(checklon)) * 1E6));
Log.i(tag, "Geo Point is...."+point);
overlayitem = new OverlayItem(point,checktitle,checksdateformate);
abc = imgloader.getBitmap(checkimg);
d = new BitmapDrawable(abc);
itemizedoverlay = new CheckInMapItemizedOverlay(d,mapview,true,streventid);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapController.animateTo(point);
}