0

我试图在来自 SQLite db 的谷歌地图上添加标记我有一个列表视图,我可以在其中看到来自 db 的纬度和经度。所以我知道数据库正在工作。我是 java 和 android 的新手。但我希望越来越好。我不确定我的代码是否正确,需要一些帮助。这是我写的代码:

列出 mapOverlays = mapView.getOverlays();

 Drawable herUAre = this.getResources().getDrawable(R.drawable.ic_launcher);

 CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(herUAre);

 OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);

 currentLocationOverlay.addOverlay(currentLocation);

 mapOverlays.clear();

 mapOverlays.add(currentLocationOverlay);   


}

上面的代码用一个针显示当前位置它可以工作:-)

下面的代码我不确定如何开始工作??????

    public ArrayList<OverlayItem> getLocations()
    {
        ArrayList<OverlayItem> locations = new ArrayList<OverlayItem>();

        String[] tableColumns = new String[]{"_id", "address", "description", "latitude", "longitude"};

        Cursor cursor = db.query("spots", tableColumns, null, null, null, null, null);

        cursor.moveToFirst();
        do{
    String adress = (cursor.getString(cursor.getColumnIndex("adress")));
    String description = (cursor.getString(cursor.getColumnIndex("description")));
    String lat1 = (cursor.getString(cursor.getColumnIndex("latitude")));
    Double lat = Double.parseDouble(lat1);
    String long1 = (cursor.getString(cursor.getColumnIndex("longitude")));
    Double lon = Double.parseDouble(long1);

    GeoPoint point = new GeoPoint((int) (lat * 1E6),(int) (lon * 1E6));
    locations.add(new OverlayItem(point, adress, description));     

        }
        while (cursor.moveToNext());
        return locations;

    }


// missing some code here to ad to my CurrentLocationOverlay class ore do i have to make a 
//new class for this ??

这是 CurrentLocationOverlay.class

public class CurrentLocationOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    public CurrentLocationOverlay(Drawable defaultMarker) {
         super(boundCenterBottom(defaultMarker));
    }

     @Override
      protected OverlayItem createItem(int i) {
          return mOverlays.get(i);
      }

      @Override
      public int size() {
          return mOverlays.size();
      }

      public void addOverlay(OverlayItem item){
          mOverlays.add(item);
          setLastFocusedIndex(-1);
          populate(); // Calls the method createItem()
      }

      @Override
      protected boolean onTap(int arg0) {
          Log.d("Tapped", mOverlays.get(arg0).getSnippet());
          return true;
      }
    }
4

1 回答 1

0

这里示例如何使用 SQLite:

公共类 DBHelper 扩展 SQLiteOpenHelper {

public static interface POSITION extends BaseColumns {
            String TABLE_NAME = "position";
            String LATITUDE = "latitude";
            String LONGTITUDE = "longtitude";
        }

private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";

private static final String CREATE_TABLE_POSITION = CREATE_TABLE + POSITION.TABLE_NAME + " (" 
                + POSITION._ID + " INTEGER PRIMARY KEY, " + POSITION.LATITUDE + " INTEGER, " 
                + POSITION.LONGTITUDE + " INTEGER);";

private static final String DATABASE_NAME = "Pointer";
        private static final int DATABASE_VERSION = 3;

public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

@Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_POSITION);
        }

@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + POSITION.TABLE_NAME);
            onCreate(db);
        }

public void insertPosition(long latitde, long longtitude) {
            ContentValues values = new ContentValues();
            values.put(POSITION.LATITUDE, latitde);
            values.put(POSITION.LONGTITUDE, longtitude);
            getWritableDatabase().insert(POSITION.TABLE_NAME, null, values);
        }

public Cursor getAllPoints() {
            return getReadableDatabase().query(POSITION.TABLE_NAME,
                    new String[] { POSITION.LATITUDE, POSITION.LONGTITUDE }, null, null, null, null, null);
        }
    }

以及如何获得reqared坐标:

public class HelloGoogleMap extends MapActivity {

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            MapView mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);
            List&lt;Overlay&gt; mapOverlays = mapView.getOverlays();
            Drawable herUAre = this.getResources().getDrawable(R.drawable.ic_launcher);
            CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(herUAre);
            OverlayItem[] currentLocation = new OverlayItem[10];
            DBHelper db = new DBHelper(this);
            db.insertPosition(50030936, 3622452);
            db.insertPosition(52033986, 3623859);
            db.insertPosition(56034932, 3625129);
            db.insertPosition(58039959, 3627329);
            Cursor allPoints = db.getAllPoints();
            int a = 0;
            while (allPoints.moveToNext()) {
                Log.e("" + allPoints.getLong(allPoints.getColumnIndex(DBHelper.POSITION.LATITUDE)), "" 
                        + allPoints.getLong(allPoints.getColumnIndex(DBHelper.POSITION.LONGTITUDE)));
                currentLocation[a] = new OverlayItem(new GeoPoint((int) allPoints.getLong(allPoints
                        .getColumnIndex(DBHelper.POSITION.LATITUDE)), (int) allPoints.getLong(allPoints
                        .getColumnIndex(DBHelper.POSITION.LONGTITUDE))), "Current Location", null);
                currentLocationOverlay.addOverlay(currentLocation[a]);
                a++;
            }
            allPoints.close();
            db.close();

mapOverlays.clear();
            mapOverlays.add(currentLocationOverlay);
        }

@Override
        protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
            return false;
        }

}
于 2013-03-12T09:52:28.067 回答