초보 안드로이드 개발자입니다.
sqlite 에서 꺼내온 정보를 바탕으로 맵뷰에 마커를 찍어주려고 합니다.
sqlite를 불러오는 부분 까지는 무난하게 됬는데..
sqlite에서 제목과 타이틀 좌표정보를 받아와서 하려고 하는데
잘 안되네요;;
어떤식으로 for문을 돌려야 될지 전혀 감이 안옵니다..;;
stackoverflow에도 적당한 샘플이 없고 맵뷰와 sqlite 연동 응용이 잘 안되네요
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
MyItemizedOverlay itemizedOverlay;
MyItemizedOverlay itemizedOverlay2;
GPSProvider gps;
LocationManager lm;
MyLocationOverlay mMyLocationOverlay;
//DB옮기기
public SQLiteDatabase db;
public Cursor cursor;
public SimpleCursorAdapter Adapter=null;
public SimpleCursorAdapter AdapterStomach=null;
public static final String ROOT_DIR = "/data/data/com.example.firstandproject/databases/";
ProductDBHelper mHelper;
public void setDB() {
File folder = new File(ROOT_DIR);
if(folder.exists()) {
}
else {
folder.mkdirs();
Toast.makeText(this, "폴더생성", Toast.LENGTH_LONG).show();
}
AssetManager assetManager = getResources().getAssets();
File outfile = new File(ROOT_DIR+"seoul.db"); //--폰에 위치할 경로
InputStream is = null;
FileOutputStream fo = null;
long filesize = 0;
try {
// --asset 폴더 및 복사할 DB 지정
is = assetManager.open("seoul.db", AssetManager.ACCESS_BUFFER);
filesize = is.available(); //--사이즈 검증
// 파일이 없거나 패키지 폴더에 설치된 DB파일이 포함된 DB파일 보다 크기가 같지않을 경우 DB파일을 덮어 쓴다.
if (outfile.length() <= 0) {
byte[] tempdata = new byte[(int) filesize];
is.read(tempdata);
is.close();
outfile.createNewFile();
fo = new FileOutputStream(outfile);
fo.write(tempdata);
fo.close();
}
else
{
Toast.makeText(this, "db있음", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
Toast.makeText(this, "db이동실패", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
setDB();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper=new ProductDBHelper(this);
db=mHelper.getWritableDatabase();
cursor=db.rawQuery("Select * from books",null);
startManagingCursor(cursor);
cursor.moveToLast();
String str=cursor.getString(0);
Toast.makeText(this,str, Toast.LENGTH_LONG).show();
ListView list=(ListView)findViewById(R.id.list);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
mapOverlays.clear();
ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
Cursor locationCursor = db.query("books", new String[] {
"name", "address", "latitude", "longitude" }, null, null,
null, null, null);
locationCursor.moveToFirst();
do {
String title = locationCursor.getString(locationCursor.getColumnIndex("name"));
String adds = locationCursor.getString(locationCursor.getColumnIndex("address"));
int latitude = (int) (locationCursor.getDouble(locationCursor
.getColumnIndex("latitude")) * 1E6);
int longitude = (int) (locationCursor.getDouble(locationCursor
.getColumnIndex("longitude")) * 1E6);
for (int i = 0; i < title.length(); i++) {
GeoPoint point323 = new GeoPoint((int)(latitude*1E6),(int)(longitude*1E6));
OverlayItem overlayItem323 = new OverlayItem(point323, title, adds);
itemizedOverlay2.addOverlay(overlayItem323);
}
} while (locationCursor.moveToNext());
//위에서 포문을 쓰면 자꾸 죽습니다.. 포문을 안쓰면 데이터없이 돌아가고요..
어떻게해야 안죽고 잘돌아갈까요...
이부분에서 어떻게 손을 대야 될지 감이 안옵니다;;
// first overlay
하드코딩하면 잘나옵니다;;
drawable = getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
GeoPoint point = new GeoPoint((int)(37.544622*1E6),(int)(126.835136*1E6));
OverlayItem overlayItem = new OverlayItem(point, "강서 푸른들도서관",
"서울시 강서구 화곡동 1019번지");
itemizedOverlay.addOverlay(overlayItem);
GeoPoint point2 = new GeoPoint((int)(37.621504*1E6),(int)(127.082991*1E6));
OverlayItem overlayItem2 = new OverlayItem(point2, "공릉청소년문화정보도서관",
"서울시 노원구 공릉동 87-5");
itemizedOverlay.addOverlay(overlayItem2);
GeoPoint point3 = new GeoPoint((int)(51.515259*1E6),(int)(-0.086623*1E6));
OverlayItem overlayItem3 = new OverlayItem(point3, "석관동미리내도서관",
"서울시 성북구 석관동 167-6");
itemizedOverlay.addOverlay(overlayItem3);
GeoPoint point4 = new GeoPoint((int)(37.613498*1E6),(int)(127.065444*1E6));
OverlayItem overlayItem4 = new OverlayItem(point4, "동대문구정보화도서관",
"서울시 동대문구 청량리2동 206-19");
itemizedOverlay.addOverlay(overlayItem4);
mapOverlays.add(itemizedOverlay);
// second overlay
drawable2 = getResources().getDrawable(R.drawable.marker2);
itemizedOverlay2 = new MyItemizedOverlay(drawable2, mapView);
GeoPoint point333 = new GeoPoint((int)(51.513329*1E6),(int)(-0.08896*1E6));
OverlayItem overlayItem333 = new OverlayItem(point333, "Sliding Doors (1998)",
"(interiors)");
itemizedOverlay2.addOverlay(overlayItem333);
GeoPoint point444 = new GeoPoint((int)(51.51738*1E6),(int)(-0.08186*1E6));
OverlayItem overlayItem444 = new OverlayItem(point444, "Mission: Impossible (1996)",
"(Ethan & Jim cafe meeting)");
itemizedOverlay2.addOverlay(overlayItem444);
mapOverlays.add(itemizedOverlay2);
final MapController mc = mapView.getController();
mc.animateTo(point2);
mc.setZoom(16);