0

SQLite データベースから Google マップにマーカーを広告しようとしています。データベースから緯度と経度を確認できるリストビューがあります。だから私は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);   


}

上記のコードは、ピンで現在の位置を示しています IT WORKS :-)

以下のコードは、どのように仕事をするのかわかりません?????

    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 を使用する方法の例を示します。

public class 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);
        }
    }

そして、必要な座標を取得する方法:

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 に答える