我想从我的数据库中的一个名为Mission的表中获取所有位置然后我想将这些位置分配到我的地图中......我尝试了这个代码但它不起作用它只显示了一个我需要帮助的位置代码运作良好?
public class MaptoDo extends MapActivity{
MapView mv;
MapController mc;
SQLiteDatabase sql;
Cursor c;
GeoPoint p;
double [] lat;
double[]lon;
List<Overlay> listOfOverlays;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemap);
Intent in =getIntent();
mv=(MapView) findViewById(R.id.mapView);
mv.setBuiltInZoomControls(true);
mv.setSatellite(true);
sql=openOrCreateDatabase("db",0,null);
c=sql.rawQuery("select * from Mission",null);
double [] lat = new double[c.getColumnCount()];
double [] lon = new double[c.getColumnCount()];
if(c.getColumnCount()!=0){
int i=0;
int count=c.getColumnCount();
c.moveToFirst();
while(count>0){
lat[i]=c.getDouble(3);
lon[i]=c.getDouble(4);
i++;
count--;
}
c.close();
}
for(int i=0;i<lat.length;i++)
{
p=new GeoPoint((int)lat[i]*1000000,(int) lon[i]*1000000);
mc = mv.getController();
mc.animateTo(p);
mc.setZoom(8);
MapOverlay mapOverlay = new MapOverlay(p,"hi","locate new point");
listOfOverlays = mv.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mv.invalidate();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuItem itIn=menu.add("Zoom in");
itIn.setIcon(R.drawable.in);
MenuItem itOut=menu.add("Zoom out");
itOut.setIcon(R.drawable.out);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getTitle()=="Zoom in")
{
MapController mc=mv.getController();
mc.zoomIn();
}
else if(item.getTitle()=="Zoom out")
{
MapController mc=mv.getController();
mc.zoomOut();
}
return super.onOptionsItemSelected(item);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MapOverlay extends com.google.android.maps.Overlay
{
public MapOverlay(GeoPoint p, String string, String string2) {
// TODO Auto-generated constructor stub
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.redpushpin);
canvas.drawBitmap(bmp, screenPts.x-20, screenPts.y-34, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
Toast.makeText(getBaseContext(),"Location: "+p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,Toast.LENGTH_SHORT).show();
}
return false;
}
}}
从用户那里获取Mission并将它们放入DataBase表中的类在这里运行良好但是我把它放在第一个代码的更多清除中:
public class newMission extends Activity
implements OnClickListener{
SQLiteDatabase sql;
EditText e1,e2,e3,e4;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent in =getIntent();
setContentView(R.layout.newmission);
e1=(EditText) findViewById(R.id.edn1);
e2=(EditText) findViewById(R.id.edn2);
e3=(EditText) findViewById(R.id.edn3);
e4=(EditText) findViewById(R.id.edn4);
b=(Button) findViewById(R.id.btnew);
b.setOnClickListener(this);
sql=openOrCreateDatabase("db",0, null);
sql.execSQL("CREATE TABLE if not exists Mission" +
"" +
" (emp integer REFERENCES Employee2 (password)," +
"oper_n text,cust_n text,lat double," +
"long double,oper_id integer PRIMARY KEY AUTOINCREMENT)");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String opN=e1.getText().toString();
String cuN=e2.getText().toString();
String lats=e3.getText().toString();
String lons=e4.getText().toString();
try {
double lat=Double.parseDouble(lats);
double lon=Double.parseDouble(lons);
// float lat=Float.parseFloat(lats);
// float lon=Float.parseFloat(lons);
sql.execSQL("INSERT INTO Mission (oper_n,cust_n,lat,long)Values('"+opN+"','"+cuN+"'," +lat+ "," +lon+ ");");
Toast.makeText(this,"Data Inserted",2000).show();
//add to ToDo list too
}catch (NumberFormatException nfe) {
Toast.makeText(this, "Please Enter Valid Data",2000).show();
}
}
}
答案 0 :(得分:0)
错误在于此行: listOfOverlays = mv.getOverlays(); listOfOverlays.clear();
从for循环中删除此代码。而这在for循环之前。同时从for循环中删除mv.invalidate,并在循环后添加它。