2017-08-14 59 views
0

我使用Sqlite數據庫和OpenStreetMap創建應用程序。在我的數據庫中,我存儲了一些數據,如ID,建築物類型,年份,功能,緯度和經度。我試圖在數據庫上顯示OpenStreetMap標記,並使用當前GPS位置顯示一個標記。 在這一刻,我的地圖顯示我的位置和數據庫的第一條記錄。 如何在地圖上顯示所有標記? P.S.我試了一下使用谷歌發現的各種例子如何在OpenStreetMap上從sqlite顯示標記

我的地圖類

public class Mapa extends Activity implements LocationListener { 
private MapView osm; 
private MapController mc; 
private LocationManager locationManager; 
WywiadData WywiadData; 
int id_marker; 
Double Dlugosc; 
Double Szerokosc; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.map_view); 

    osm = (MapView) findViewById(R.id.mapView); 
    final float scale = getBaseContext().getResources().getDisplayMetrics().density; 
    final int newScale = (int) (256 * scale); 
    String[] OSMSource = new String[2]; 
    OSMSource[0] = "http://a.tile.openstreetmap.org/"; 
    OSMSource[1] = "http://b.tile.openstreetmap.org/"; 
    XYTileSource MapSource = new XYTileSource("OSM", null, 1, 18, newScale, ".png", OSMSource); 
    osm.setTileSource(MapSource); 
    osm.setBuiltInZoomControls(true); 
    osm.setMultiTouchControls(true); 
    osm.setMaxZoomLevel(18); 
    osm.setMinZoomLevel(1); 
    mc = (MapController) osm.getController(); 
    mc.setZoom(18); 
    GeoPoint center = new GeoPoint(50.2586, 19.0223); 
    mc.animateTo(center); 
    WywiadData = new WywiadData(this, null, null, 1); 
    Cursor cursor = WywiadData.getMarkers(); 
    Double Dlq = cursor.getDouble(cursor.getColumnIndexOrThrow("Dlugosc")); 
    Double Szr = cursor.getDouble(cursor.getColumnIndexOrThrow("Szerokosc")); 
    id_marker = cursor.getInt(cursor.getColumnIndexOrThrow("_id")); 
    Dlugosc = Double.valueOf(Dlq); 
    Szerokosc = Double.valueOf(Szr); 


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     return; 
    } 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
} 

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
public void addMarker(GeoPoint center) { 
    Marker marker = new Marker(osm); 
    marker.setPosition(center); 
    Marker marker2 = new Marker(osm); 
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM); 
    GeoPoint center2 = new GeoPoint(Dlugosc, Szerokosc); 
    marker2.setPosition(center2); 
    marker2.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM); 
    osm.getOverlays().add(marker); 
    osm.getOverlays().add(marker2); 
} 


@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
@Override 
public void onLocationChanged(Location location) { 
    GeoPoint center = new GeoPoint(location.getLatitude(), location.getLongitude()); 
    mc.animateTo(center); 
    osm.getOverlays().clear(); 
    addMarker(center); 
    osm.invalidate(); 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 
} 

而我的DB類的事:

public class WywiadData extends SQLiteOpenHelper { 
public static final String DATA_BASE_NAME = "WYWIAD.db"; 
public static final int DATA_BASE_VERSION = 1; 
public final static String TAG = "Wywiad Data"; 

public WywiadData(Context context, Object o, Object o1, int i) { 
    super(context, DATA_BASE_NAME, null, DATA_BASE_VERSION); 
} 

public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE " + Tabela.TABLE_NAME + " (" + Tabela._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Tabela.OPERATOR + " TEXT, " + Tabela.FUNKCJA + " TEXT, " + Tabela.KOD_FUNKCJI + " TEXT, " + Tabela.MATERIAL + " TEXT, " + Tabela.ROK + " INTEGER, " + Tabela.ZRODLO + " TEXT, " + Tabela.KOND_NADZ + " INTEGER, " + Tabela.KOND_PODZ + " INTEGER, " + Tabela.STATUS + " TEXT, " + Tabela.DLUGOSC + " TEXT, " + Tabela.SZEROKOSC + " TEXT, " + Tabela.DATA + " TEXT, " + Tabela.UWAGI + " TEXT" + ")"); 
} 

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

public boolean insertData(String Operator, String Funkcja, String Kod, String Material, String Rok, String Zrodlo, String Kond_nadz, String Kond_podz, String Status, String DataDodania, Double Dlugosc, Double Szerkosc, String Uwagi) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(Tabela.OPERATOR, Operator); 
    contentValues.put(Tabela.FUNKCJA, Funkcja); 
    contentValues.put(Tabela.KOD_FUNKCJI, Kod); 
    contentValues.put(Tabela.MATERIAL, Material); 
    contentValues.put(Tabela.ROK, Rok); 
    contentValues.put(Tabela.ZRODLO, Zrodlo); 
    contentValues.put(Tabela.KOND_NADZ, Kond_nadz); 
    contentValues.put(Tabela.KOND_PODZ, Kond_podz); 
    contentValues.put(Tabela.STATUS, Status); 
    contentValues.put(Tabela.DATA, DataDodania); 
    contentValues.put(Tabela.DLUGOSC, Dlugosc); 
    contentValues.put(Tabela.SZEROKOSC, Szerkosc); 
    contentValues.put(Tabela.UWAGI, Uwagi); 
    long result = db.insert(Tabela.TABLE_NAME, null, contentValues); 
    if (result == -1) { 
     return false; 
    } else { 
     return true; 
    } 
} 

public Cursor getListContents() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor data = db.rawQuery(" SELECT * FROM " + Tabela.TABLE_NAME, null); 
    return data; 
} 


public Cursor getItemID(int id) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    String query = " SELECT * FROM " + Tabela.TABLE_NAME + " WHERE " + Tabela._ID + " = '" + id + "'"; 
    Cursor cursor = db.rawQuery(query, null); 
    if (cursor != null) { 
     cursor.moveToFirst(); 

    } 
    Log.e("Wywiad", "zwrócono po id: " + cursor); 
    return cursor; 
} 


public void updateData(Lista lista) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(Tabela.OPERATOR, lista.getOperator()); 
    contentValues.put(Tabela.FUNKCJA, lista.getfunkcja()); 
    contentValues.put(Tabela.KOD_FUNKCJI, lista.getKod()); 
    contentValues.put(Tabela.MATERIAL, lista.getmaterial()); 
    contentValues.put(Tabela.ROK, lista.getrok()); 
    contentValues.put(Tabela.ZRODLO, lista.getzrodlo()); 
    contentValues.put(Tabela.KOND_NADZ, lista.getKondygnacje_nad()); 
    contentValues.put(Tabela.KOND_PODZ, lista.getKondygnacje_pod()); 
    contentValues.put(Tabela.STATUS, lista.getstatus()); 
    contentValues.put(Tabela.UWAGI, lista.getuwagi()); 
    db.update(Tabela.TABLE_NAME, contentValues, Tabela._ID + "=?", new String[]{String.valueOf(lista.getIdentyfikator())}); 
    db.close(); 

} 

public void deleteData(int id) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    String query = " DELETE FROM " + Tabela.TABLE_NAME + " WHERE " + Tabela._ID + " = '" + id + "'"; 
    db.execSQL(query); 
} 

public Cursor getMarkers() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(" SELECT * FROM " + Tabela.TABLE_NAME, null); 
    if (cursor != null) { 
     cursor.moveToFirst(); 

    } 
    return cursor; 
} 
} 

回答

0

其實你不創建一個標記,你要什麼do是使用MarkerOptions定義Marker的特徵,然後將其添加到Map中

public void addMarker(GeoPoint center) { 
    MarkerOptions options = new MarkerOptions(); 
    Marker marker = new Marker(osm); 
    options.position(center); 
    options.anchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM); 

    Marker marker = osm.AddMarker(options); 
} 

編輯 從sqlite的

try { 
    while (cursor.moveToNext()) { 
     GeoPoint point = new GeoPoint(cursor.getLong("lat"), cursor.getLong("lng")); 
     addMarker(point); 
    } 
} finally { 
    cursor.close(); 
} 

這將通過所有光標對象的迭代,創建一個GeoPoint,然後將它傳遞給addMarker方法

+0

所以,我怎麼能顯示所有標記添加所有從sqlite?你能解釋給我嗎?我很抱歉,我剛剛開始編程,現在對我來說太困難了。 –