0

i want to get all the locations from a table Called Mission in my database then i want to assign these locations into my map ...i tried this code but it does not work it shows only one location i need help for this code to work well ?

  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;

    }
}}

the class that takes the Mission from the user and put them into the table of the DataBase is here it works well but i put it for more clearance of the first code :

   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();
}
     }

}

4

1 回答 1

0

错误在于这一行:listOfOverlays = mv.getOverlays(); listOfOverlays.clear();

从 for 循环中删除此代码。这在 for 循环之前。还要从 for 循环中删除 mv.invalidate 并将其添加到循环之后。

于 2012-08-20T06:46:32.873 回答